Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog SelfLog to File Error

I need to enable self log of seri logger to text file. My configuration are follows;

__serilogLogger = new LoggerConfiguration()
      .Enrich.WithProperty("ApplicationIPv4", _ipv4)
      .Enrich.WithProperty("ApplicationIPv6", _ipv6)
      .WriteTo.MSSqlServer(connectionString, tableName /*, columnOptions: columnOptions*/)
      .WriteTo
      .Seq(ConfigurationManager.AppSettings["SerilogServer"])
      .CreateLogger();

      var file = File.CreateText("Self.log");
      Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));

But is hows File access error when run the application. Please find the error details below;

Additional information: The process cannot access the file 'C:\Program Files (x86)\IIS Express\Self.log' because it is being used by another process.

Can anyone help me on this

like image 551
Akhil Avatar asked Nov 25 '25 00:11

Akhil


1 Answers

try

Serilog.Debugging.SelfLog.Enable(msg => File.AppendAllText (serilogSelfLogFilePath, msg));

for a non-locking way to write to a file. serilogSelfLogFilePath is a valid path string to the file you want to use.

Don't forget Log.CloseAndFlush(); when you're closing your other logs per https://github.com/serilog/serilog/issues/864 or it won't get written anyway

Note, if you are using an async Sink then you will eventually get an exception when different threads contend to write to that file if it gets busy.

like image 60
CAD bloke Avatar answered Nov 27 '25 13:11

CAD bloke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!