-1

I'm using .Net Core 3.1 Application which is API. Due to below code, I'm able to generate example.txt log file however unable to delete this. "The process cannot access the file '' because it is being used by another process w3p".

Log.Logger = (new LoggerConfiguration())
                .MinimumLevel.Information()
                .MinimumLevel.Override("SerilogDemo", LogEventLevel.Information)
                .WriteTo.File("C:\\Logs\\Example.txt", 
                    restrictedToMinimumLevel:LogEventLevel.Information, 
                    fileSizeLimitBytes: 2000,
                    retainedFileCountLimit:5, 
                    rollOnFileSizeLimit:true ),
                .CreateLogger();
Kjartan
  • 18,591
  • 15
  • 71
  • 96

1 Answers1

0

I'm not certain about your specific code, but the core principle seems clear enough: You've created and opened a file for writing, and by doing so you have in essence created a "hold" on the file so that it can't be deleted.

The process called w3p is the process that your service runs under; if you stop the server or the service, you should be able to delete the file manually, if you wanted to - but I'm guessing you want to do it programatically?

In any case, what you probably need to do is dispose of the logger (Log.Logger), since that refers to file. As long as the logger exists, it will likely keep the connection to the file open.

Kjartan
  • 18,591
  • 15
  • 71
  • 96