SelfLog not writing to Text File

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
tsimbalarcommented, Jan 17, 2019

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 …

0reactions
filmicocommented, Jul 11, 2021

Another Sample that append the Serilog Internal Errors to a file

Serilog.Debugging.SelfLog.Enable(message => {

  // 	C# StreamWriter Example
  // https://www.c-sharpcorner.com/article/csharp-streamwriter-example/

  var filePath = Server.MapPath("~/Logs/SerilogInternalErrors.log");

  using (var sw = new StreamWriter(filePath, append: true, Encoding.UTF8))
  {
      sw.WriteLine(message);
  }

  Log.CloseAndFlush();

});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serilog SelfLog to File Error
try. Serilog.Debugging.SelfLog.Enable(msg => File.AppendAllText (serilogSelfLogFilePath, msg));. for a non-locking way to write to a file.
Read more >
Troubleshoot no logging using Serilog
Tried to enable the SelfLog to log in the console while debugging but I get no message there either. Any clue about how...
Read more >
Ingestion Troubleshooting
1. Check Serilog's SelfLog. If your applications are unable to reach or authenticate to Seq, and you're using Serilog ...
Read more >
Serilog Tutorial for .NET Logging: 16 Best Practices and Tips
Serilog makes it easy to record custom object properties and output your logs to JSON. Read this Serilog tutorial for best practices and ......
Read more >
Logging — PyTorch Lightning 2.0.7 documentation
Log to local file system in TensorBoard format. ... If you want to log anything that is not a scalar, like histograms, text,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found