1

How would I add in an outputTemplate into this code?

Serilog.Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .Enrich.WithMachineName()
                .WriteTo.Sink(new RollingFileSink(applicationLogsPath, new JsonFormatter(), 1000000000, 10))
                .CreateLogger();
OutOFTouch
  • 1,017
  • 3
  • 13
  • 30

1 Answers1

0

If your goal is to write human-readable text into the file rather than JSON, you can just use WriteTo.RollingFile():

Serilog.Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .Enrich.WithMachineName()
            .WriteTo.RollingFile(
                 applicationLogsPath,
                 outputTemplate: "<output template goes here>",
                 fileSizeLimitBytes: 1000000000,
                 retainedFileCountLimit: 10)
            .CreateLogger();
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • I am looking for how to add the outputTemplate with the code I posted with a JSON formatter. – OutOFTouch Aug 24 '15 at 14:06
  • Serilog.Log.Error("{@Request} {@ExceptionContext} {Message} {StackTrace} {Exception}", request, exceptionContext, context.Exception.Message, context.Exception.StackTrace, context.Exception); I want something like this but I want to define the template at the time I define the Logger. – OutOFTouch Aug 25 '15 at 18:52
  • I see - you want to format the message itself within the JSON? Serilog's output formatters are exclusive, you either log text using an output template, or JSON in which case the output template is redundant: the properties of the log event will be first class properties in JSON, no need to format into the message text. – Nicholas Blumhardt Aug 26 '15 at 07:43
  • I am confused because I use the code I posted on my question to create the logger now and then inside of an ExceptionLogger I use the code I posted on my comment to tell Serilog what properties to log, I just want to be able to do this at the time of the declaration of the logger itself, and the result I get in the text file is JSON. – OutOFTouch Aug 26 '15 at 14:04
  • Do you understand what I am asking? – OutOFTouch Aug 31 '15 at 21:12
  • https://github.com/Leftyx/OwinWebApiSelfHost/blob/master/OwinWebApiSelfHost/Infrastructure/ExceptionHandling/SerilogExceptionLogger.cs I am currently logging almost like this guys code except instead of separate calls to Log.Error for Request, ExceptionContext and StackTrace I have it all on one line like a template and my JSON logger logs it as json, how can I do this by defining the template at the time I define the logger. – OutOFTouch Sep 02 '15 at 20:48