Created
April 4, 2016 17:44
-
-
Save Mart-Bogdan/cafa9433ae6739bf9085e2a4077abffd to your computer and use it in GitHub Desktop.
Serilog Sample
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
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(); | |
} |
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
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; | |
} |
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
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