I'm using log4net in a ASP.NET MVC 4 application and I'm trying to download the generated log file from log4Net.
I have in mind to download the log file with a FileResult, like:
[Authorize(Roles = "Admin")]
public FileResult DownloadUserInterfaceLog()
{
// Get the path of the log file
string path = (LogManager.GetCurrentLoggers()[0].Logger.Repository.GetAppenders()[0] as FileAppender).File;
// Get the file
byte[] fileBytes = System.IO.File.ReadAllBytes(path);
string fileName = "Log.txt";
// Return the expected file
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
My problem is that when I call System.IO.File.ReadAllBytes(path);
I get an Exception, cause the log file is already used by another process.
I'm already put <lockingModel value="log4net.Appender.FileAppender+MinimalLock" /> (which should release the file after any modification) in my Web.Config
with no success.
Begin of log4Net configuration in Web.Config:
<log4net>
<appender name="Appender-root" type="log4net.Appender.RollingFileAppender">
<lockingModel value="log4net.Appender.FileAppender+MinimalLock" />
<file value="Path..." />
<appendToFile value="true" />