1 using Microsoft.Extensions.Configuration;
2 using Microsoft.Extensions.DependencyInjection;
3 using Microsoft.Extensions.Logging;
21 public const string SerilogContextTemplate =
"(Instance:{Instance}|Job:{Job}|Request:{Request}|User:{User}|Monitor:{Monitor}|Bridge:{Bridge}|Chat:{ChatMessage})";
30 public static IServiceCollection UseStandardConfig<TConfig>(
this IServiceCollection serviceCollection, IConfiguration configuration) where TConfig :
class 32 if (serviceCollection == null)
33 throw new ArgumentNullException(nameof(serviceCollection));
34 if (configuration == null)
35 throw new ArgumentNullException(nameof(configuration));
39 var configType = typeof(TConfig);
40 var sectionField = configType.GetField(SectionFieldName);
41 if (sectionField == null)
42 throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
"{0} has no {1} field!", configType, SectionFieldName));
44 var stringType = typeof(
string);
45 if (sectionField.FieldType != stringType)
46 throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
"{0} has invalid {1} field type, must be {2}!", configType, SectionFieldName, stringType));
48 var sectionName = (string)sectionField.GetValue(null);
50 return serviceCollection.Configure<TConfig>(configuration.GetSection(sectionName));
60 public static IServiceCollection SetupLogging(
61 this IServiceCollection serviceCollection,
62 Action<LoggerConfiguration> configurationAction,
63 Action<LoggerSinkConfiguration> sinkConfigurationAction = null)
64 => serviceCollection.AddLogging(builder =>
66 builder.ClearProviders();
68 var configuration =
new LoggerConfiguration()
72 configurationAction?.Invoke(configuration);
75 .Enrich.FromLogContext()
77 .Async(sinkConfiguration =>
79 sinkConfiguration.Console(
80 outputTemplate:
"[{Timestamp:HH:mm:ss}] {Level:w3}: {SourceContext:l} " 81 + SerilogContextTemplate
82 +
"{NewLine} {Message:lj}{NewLine}{Exception}");
83 sinkConfigurationAction?.Invoke(sinkConfiguration);
86 builder.AddSerilog(configuration.CreateLogger(),
true);
88 if (Debugger.IsAttached)
Use server authentication
const string Section
The key for the Microsoft.Extensions.Configuration.IConfigurationSection the GeneralConfiguration res...
Extensions for IServiceCollection
General configuration options