Serilog: SelfLog not writing to Text File

Created on 16 Jan 2019  ·  4Comments  ·  Source: serilog/serilog

I'm trying to debug an issue where 'some' of our logs never appear in LogAnalytics using the Serilog.Sinks.AzureAnalytics (see issue here https://github.com/saleem-mirza/serilog-sinks-azure-analytics/issues/42).

.NET Core 2.1 api,
using:
Serilog.AspNetCore 2.1.1
Serilog.Sinks.AzureAnalytics 4.0.0

Based on your wiki under the Debug and Diagnose section I'm doing the following as the first statements in my startup.cs:

var file = File.CreateText("myFilePath"); Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file)); Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg)); Serilog.Debugging.SelfLog.Enable(Console.Error);
The other debug outputs work fine and I can see the batch logs, but the logging to the file is empty.
The file does indeed get created, it's just empty.

I've seen other issues on here stating to use Log.CloseAndFlush(); but I can't do this as this is a continuously running api.

Am I missing something vital?

Most helpful comment

Hi,

Every time you call SelfLog.Enable(...) , you replace previous Selflog error handler ...

Which means that :

var file = File.CreateText("myFilePath"); Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
// right now SelfLog should write to file ...
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg)); 
// right now selfLog should write to Debug, but no longer to file
Serilog.Debugging.SelfLog.Enable(Console.Error);
// right now selflog should write to Console.Error, but no longer to Debug or File ...

If you want the SelfLog to write to multiple outputs, you need to do it all in a single SelfLog.Enable() call like :

var writer = TextWriter.Synchronized(file);
SelfLog.Enable(msg => {
  Debug.WriteLine(msg);
  Console.Error(msg);
  writer.WriteLine(msg);
  writer.Flush();
});

I hope this helps !

(you may also want to try/catch around the writing to file just in case ...

All 4 comments

Hi @sicollins - thanks for getting in touch.

SelfLog will only show messages/errors raised in the Serilog pipeline itself - it won't get a copy of the log events that are being sent through the sinks.

For errors to make it into SelfLog, sinks like the Azure Analytics one need to explicitly write them, so some additional instrumentation might be needed within that sink itself in order to surface the issue.

Hope this helps,
Nick

Thanks @nblumhardt - I'm not trying to get serilog to pump out the usual log messages, more the output of it's internal logging when it sends batches to the Azure data collection api. It writes these out to the console, just doesn't write to Textfile like it's meant to. Not sure if this is an issue with serilog or the sink itself. I'll post message on that repo about it too. Cheers

Hi,

Every time you call SelfLog.Enable(...) , you replace previous Selflog error handler ...

Which means that :

var file = File.CreateText("myFilePath"); Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
// right now SelfLog should write to file ...
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg)); 
// right now selfLog should write to Debug, but no longer to file
Serilog.Debugging.SelfLog.Enable(Console.Error);
// right now selflog should write to Console.Error, but no longer to Debug or File ...

If you want the SelfLog to write to multiple outputs, you need to do it all in a single SelfLog.Enable() call like :

var writer = TextWriter.Synchronized(file);
SelfLog.Enable(msg => {
  Debug.WriteLine(msg);
  Console.Error(msg);
  writer.WriteLine(msg);
  writer.Flush();
});

I hope this helps !

(you may also want to try/catch around the writing to file just in case ...

@tsimbalar that did the trick, thanks. Also had to hook up an OnShutdown event in my app startup.cs to flush and close the file when the app terminates.

Was this page helpful?
0 / 5 - 0 ratings