Skip to content

Instantly share code, notes, and snippets.

@Mart-Bogdan
Created April 4, 2016 17:44
Show Gist options
  • Save Mart-Bogdan/cafa9433ae6739bf9085e2a4077abffd to your computer and use it in GitHub Desktop.
Save Mart-Bogdan/cafa9433ae6739bf9085e2a4077abffd to your computer and use it in GitHub Desktop.
Serilog Sample
private void InitSerilog()
{
var logPath=Context.Server.MapPath(@"~/Logs");
var factory = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.With(new HttpRequestIdEnricher())
.MinimumLevel.Verbose();
//.WriteTo.ColoredConsole(
////outputTemplate: "{Timestamp:o} [{Level}] [{SourceContext:l}] ({ThreadId}) [{NDC}]{NewLine}{Message}{NewLine}{Exception:l}"
// outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level}] [{SourceContext:l}] ({ThreadId}) {Message}{NewLine}{Exception:l}"
//)
try
{
Directory.CreateDirectory(logPath);
factory.WriteTo.RollingFile(
pathFormat: logPath + "\\my-site-{Date}.txt",
outputTemplate:
"{Timestamp:HH:mm:ss.fff} [{Level}] [{SourceContext:l}] ({ThreadId}) {Message}{NewLine}{Exception:l}"
)
.WriteTo.Sink(new RollingFileSink(logPath + "\\my-site-{Date}.json", new JsonFormatter(), null, null))
.WriteTo.Trace()
.WriteTo.Seq("http://localhost:5341/");
}catch{}
Log.Logger = factory.CreateLogger();
}
private static void InitSerilog()
{
var logPath = Assembly.GetEntryAssembly().GetFiles().First().Name;
logPath = Path.GetDirectoryName(logPath) + "\\Logs";
Directory.CreateDirectory(logPath);
var log = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.MinimumLevel.Verbose()
.WriteTo.ColoredConsole(
//outputTemplate: "{Timestamp:o} [{Level}] [{SourceContext:l}] ({ThreadId}) [{NDC}]{NewLine}{Message}{NewLine}{Exception:l}"
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level}] [{SourceContext:l}] ({ThreadId}) {Message}{NewLine}{Exception:l}"
)
.WriteTo.RollingFile(
pathFormat: logPath + "\\my-site-{Date}.txt",
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level}] [{SourceContext:l}] ({ThreadId}) {Message}{NewLine}{Exception:l}"
)
.WriteTo.Sink(new RollingFileSink(logPath + "\\my-site-{Date}.json", new JsonFormatter(), null, null))
.CreateLogger();
// set global instance of Serilog logger which Common.Logger.Serilog requires.
Serilog.Log.Logger = log;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
Serilog.Ilogger log = Serilog.Log.Logger.ForContext(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment