builder.Register(c => SerilogConfig.CreateLogger()) .As<ILogger>() .SingleInstance();
builder.RegisterModule<SerilogLoggingModule>();
public class SerilogLoggingModule : Module { private static void AddILoggerToParameters(object sender, PreparingEventArgs e) { var t = e.Component.Activator.LimitType; e.Parameters = e.Parameters.Union( new[] { new ResolvedParameter((p, i) => p.ParameterType == typeof(ILogger), (p, i) => i.Resolve<ILogger>().ForContext(t)), }); }
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration) { registration.Preparing += AddILoggerToParameters; } }
public class SerilogConfig { public static ILogger CreateLogger() { var config = new LoggerConfiguration(). MinimumLevel.Debug(). WriteTo.ColoredConsole(LogEventLevel.Debug). WriteTo.Sink(new RollingFileSink("log-debug-{Date}.json", new JsonFormatter(renderMessage: true), null, null), LogEventLevel.Debug). WriteTo.RollingFile("log-debug-{Date}.log", LogEventLevel.Debug). WriteTo.Sink(new RollingFileSink("log-{Date}.json", new JsonFormatter(renderMessage: true), null, null), LogEventLevel.Warning);
InitialiseGlobalContext(config);
return config.CreateLogger(); }
public static LoggerConfiguration InitialiseGlobalContext(LoggerConfiguration configuration) { return configuration.Enrich.WithMachineName() .Enrich.WithProperty("ApplicationName", typeof(SerilogConfig).Assembly.GetName().Name) .Enrich.WithProperty("UserName", Environment.UserName) .Enrich.WithProperty("AppDomain", AppDomain.CurrentDomain) .Enrich.WithProperty("RuntimeVersion", Environment.Version) // this ensures that calls to LogContext.PushProperty will cause the logger to be enriched .Enrich.FromLogContext(); } }
--
You received this message because you are subscribed to the Google Groups "Serilog" group.
To unsubscribe from this group and stop receiving emails from it, send an email to serilog+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.