public static void Main(string[] args) { var loggerFactory = new LoggerFactory(); // This will write (1) to console with colors, and (2) to a rolling file with a custom formatting. loggerFactory.AddSerilog( new LoggerConfiguration() .WriteTo.ColoredConsole() .WriteTo.RollingFile( "dailylogs.log", Serilog.Events.LogEventLevel.Warning, "@{Timestamp:HH:mm:ss.fff} --- {Message}{NewLine}{Exception}{NewLine}") ); var log = loggerFactory.Create("My test app"); log.WriteVerbose("This is verbose -- won't show up by default"); log.WriteInformation("This is just for your information"); log.WriteCritical("Something critical"); log.WriteWarning("just a warning"); log.WriteCritical("Another critical event, causing an exception", new Exception("testException")); }
public Program() { // a DI based application would get ILoggerFactory injected instead var factory = new LoggerFactory(); // getting the logger immediately using the class's name is conventional _logger = factory.CreateLogger(typeof(Program).FullName); // providers may be added to an ILoggerFactory at any time, existing ILoggers are updated #if !DNXCORE50 factory.AddNLog(new global::NLog.LogFactory()); factory.AddSerilog(new Serilog.LoggerConfiguration() .Enrich.WithMachineName() .Enrich.WithProcessId() .Enrich.WithThreadId() .MinimumLevel.Debug() .WriteTo.RollingFile("file-{Date}.txt", outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {Level}:{EventId} [{SourceContext}] {Message}{NewLine}{Exception}") .WriteTo.Sink(new RollingFileSink("file-{Date}.json", new JsonFormatter(), null, null)) .WriteTo.Sink(new FileSink("dump.txt", new RawFormatter(), null))); #endif factory.AddConsole(); factory.AddConsole((category, logLevel) => logLevel >= LogLevel.Critical && category.Equals(typeof(Program).FullName)); }