-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
374 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
using System.Collections; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Sholo.Utils | ||
{ | ||
public abstract class BaseServiceCollectionExtender : IServiceCollection | ||
{ | ||
public int Count => Target.Count; | ||
public bool IsReadOnly => Target.IsReadOnly; | ||
namespace Sholo.Utils; | ||
|
||
public ServiceDescriptor this[int index] | ||
{ | ||
get => Target[index]; | ||
set => Target[index] = value; | ||
} | ||
[PublicAPI] | ||
public abstract class BaseServiceCollectionExtender : IServiceCollection | ||
{ | ||
public int Count => Target.Count; | ||
public bool IsReadOnly => Target.IsReadOnly; | ||
|
||
protected IServiceCollection Target { get; } | ||
public ServiceDescriptor this[int index] | ||
{ | ||
get => Target[index]; | ||
set => Target[index] = value; | ||
} | ||
|
||
protected BaseServiceCollectionExtender(IServiceCollection target) | ||
{ | ||
Target = target; | ||
} | ||
protected IServiceCollection Target { get; } | ||
|
||
public IEnumerator<ServiceDescriptor> GetEnumerator() => Target.GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => Target.GetEnumerator(); | ||
public void Add(ServiceDescriptor item) => Target.Add(item); | ||
public void Clear() => Target.Clear(); | ||
public bool Contains(ServiceDescriptor item) => Target.Contains(item); | ||
public void CopyTo(ServiceDescriptor[] array, int arrayIndex) => Target.CopyTo(array, arrayIndex); | ||
public bool Remove(ServiceDescriptor item) => Target.Remove(item); | ||
public int IndexOf(ServiceDescriptor item) => Target.IndexOf(item); | ||
public void Insert(int index, ServiceDescriptor item) => Target.Insert(index, item); | ||
public void RemoveAt(int index) => Target.RemoveAt(index); | ||
protected BaseServiceCollectionExtender(IServiceCollection target) | ||
{ | ||
Target = target; | ||
} | ||
} | ||
|
||
public IEnumerator<ServiceDescriptor> GetEnumerator() => Target.GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => Target.GetEnumerator(); | ||
public void Add(ServiceDescriptor item) => Target.Add(item); | ||
public void Clear() => Target.Clear(); | ||
public bool Contains(ServiceDescriptor item) => Target.Contains(item); | ||
public void CopyTo(ServiceDescriptor[] array, int arrayIndex) => Target.CopyTo(array, arrayIndex); | ||
public bool Remove(ServiceDescriptor item) => Target.Remove(item); | ||
public int IndexOf(ServiceDescriptor item) => Target.IndexOf(item); | ||
public void Insert(int index, ServiceDescriptor item) => Target.Insert(index, item); | ||
public void RemoveAt(int index) => Target.RemoveAt(index); | ||
} |
66 changes: 66 additions & 0 deletions
66
Source/Sholo.Utils/Collections/LazyConcurrentDictionaryThing.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
|
||
namespace Sholo.Utils.Collections; | ||
|
||
[PublicAPI] | ||
public class LazyConcurrentDictionaryThing<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> | ||
where TKey : notnull | ||
{ | ||
private ConcurrentDictionary<TKey, Lazy<TValue>> Dictionary { get; } = new(); | ||
|
||
public TValue GetOrAdd(TKey key, Func<TValue> valueFactory) => Dictionary.GetOrAdd(key, _ => new Lazy<TValue>(valueFactory, LazyThreadSafetyMode.ExecutionAndPublication)).Value; | ||
|
||
public TValue this[TKey key] => Dictionary[key].Value; | ||
public bool ContainsKey(TKey key) => Dictionary.ContainsKey(key); | ||
|
||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => Dictionary.Select(x => new KeyValuePair<TKey, TValue>(x.Key, x.Value.Value)).GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
public bool Remove(TKey key, out TValue value) | ||
{ | ||
var result = Dictionary.Remove(key, out var val); | ||
|
||
if (result) | ||
{ | ||
value = val.Value; | ||
return true; | ||
} | ||
else | ||
{ | ||
value = default!; | ||
return false; | ||
} | ||
} | ||
|
||
public bool IsFixedSize => false; | ||
public bool IsReadOnly => false; | ||
|
||
public void Clear() | ||
{ | ||
Dictionary.Clear(); | ||
} | ||
|
||
public int Count => Dictionary.Count; | ||
|
||
public bool TryGetValue(TKey key, out TValue value) | ||
{ | ||
if (Dictionary.TryGetValue(key, out var lazyValue)) | ||
{ | ||
value = lazyValue.Value; | ||
return true; | ||
} | ||
else | ||
{ | ||
value = default; | ||
return false; | ||
} | ||
} | ||
|
||
public ICollection<TKey> Keys => Dictionary.Keys; | ||
public ICollection<TValue> Values => Dictionary.Values.Select(x => x.Value).ToArray(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Global using directives | ||
|
||
global using JetBrains.Annotations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Diagnostics.Metrics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Sholo.Utils; | ||
|
||
public interface IMeters | ||
{ | ||
Meter Meter { get; } | ||
|
||
TInstrument GetOrAddInstrument<TInstrument, T>(Func<Instrument<T>> valueFactory, [CallerMemberName] string name = null) | ||
where TInstrument : Instrument<T> | ||
where T : struct; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Serilog; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using Serilog.Sinks.SystemConsole.Themes; | ||
|
||
namespace Sholo.Utils.Logging; | ||
|
||
public static class LoggingBuilderExtensions | ||
{ | ||
public static ILoggingBuilder AddSerilogConsole(this ILoggingBuilder loggingBuilder, HostBuilderContext hostBuilderContext) | ||
{ | ||
var configuration = hostBuilderContext.Configuration; | ||
return AddSerilogConsole(loggingBuilder, configuration); | ||
} | ||
|
||
public static ILoggingBuilder AddSerilogConsole(this ILoggingBuilder loggingBuilder, IConfiguration configuration) | ||
{ | ||
Logger logger; | ||
var serilogKeys = configuration.GetSection("Serilog").AsEnumerable().ToArray(); | ||
if (serilogKeys.Any(x => x.Key != "Serilog")) | ||
{ | ||
logger = new LoggerConfiguration() | ||
.ReadFrom.Configuration(configuration) | ||
.Filter.ByExcluding(DefaultFilters) | ||
.CreateLogger(); | ||
|
||
loggingBuilder.AddSerilog(logger, true); | ||
} | ||
else | ||
{ | ||
logger = new LoggerConfiguration() | ||
.Filter.ByExcluding(DefaultFilters) | ||
.WriteTo.Console( | ||
LogEventLevel.Debug, | ||
"[{Level:u3}] {Message:lj}{NewLine}{Exception}", | ||
standardErrorFromLevel: LogEventLevel.Warning, | ||
theme: AnsiConsoleTheme.Literate) | ||
.CreateLogger(); | ||
|
||
loggingBuilder.AddSerilog(logger, true); | ||
} | ||
|
||
return loggingBuilder; | ||
} | ||
|
||
private static bool DefaultFilters(LogEvent le) | ||
{ | ||
var sourceContext = le.Properties["SourceContext"]?.ToString() ?? string.Empty; | ||
|
||
if (sourceContext.Contains("Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager", StringComparison.Ordinal) && le.Level < LogEventLevel.Warning) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Diagnostics.Metrics; | ||
using System.Runtime.CompilerServices; | ||
using Sholo.Utils.Collections; | ||
|
||
namespace Sholo.Utils; | ||
|
||
[PublicAPI] | ||
public class Meters : IMeters | ||
{ | ||
public Meter Meter { get; } | ||
private LazyConcurrentDictionaryThing<string, Instrument> Instruments { get; } = new(); | ||
|
||
public TInstrument GetOrAddInstrument<TInstrument, T>(Func<Instrument<T>> valueFactory, [CallerMemberName] string name = null) | ||
where TInstrument : Instrument<T> | ||
where T : struct | ||
{ | ||
var result = Instruments.GetOrAdd(name, valueFactory); | ||
return (TInstrument)result; | ||
} | ||
|
||
public Meters(string applicationName, [CanBeNull] string version) | ||
{ | ||
Meter = new(applicationName, version); | ||
} | ||
} |
Oops, something went wrong.