serilog: Rollingfile relative path not working in ASP.NET MVC + IIS Express

I’m using the serilog on first application, it’s ASP.NET MVC app + IIS Express using serilog, configured with following AppSetting configs,

<add key="serilog:minimum-level" value="Verbose" />
<add key="serilog:write-to:RollingFile.pathFormat" value="logs\log-{Date}.txt" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="3" />

the application creates log file at C:\Program Files (x86)\IIS Express\logs\

Is there a way to to use the relative path. It works if i use a full path!

I’m using log4net in other MVC apps, it resolves to relative path, though!

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 21 (4 by maintainers)

Most upvoted comments

Folks, if anyone has ideas for baking this in more elegantly I’d be interested; for now I think the following in Global.asax.cs before configuring the logger:

Environment.SetEnvironmentVariable("BASEDIR", AppDomain.CurrentDomain.BaseDirectory);

and the following config:

<add key="serilog:write-to:RollingFile.pathFormat" value="%BASEDIR%\logs\log-{Date}.txt" />

is a pretty good option. @SathishN thanks again for the input on this!

Here is how I did it with an ASP.NET MVC 4/5 application.

Add the following to the web.config file:

<add key="serilog:minimum-level" value="Information" />
<add key="serilog:minimum-level:override:Microsoft" value="Information" />
<add key="serilog:minimum-level:override:System" value="Information" />
<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
<add key="serilog:write-to:RollingFile.pathFormat" value="./Logs/log-{Date}.txt" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />

Then add the following to the Application_Start in Global.asax:

// Get application base directory
string basedir = AppDomain.CurrentDomain.BaseDirectory;

// Setup Serilog for logging
Log.Logger = new LoggerConfiguration()
            .ReadFrom.AppSettings()
            .WriteTo.RollingFile(basedir + "/Logs/log-{Date}.txt")
            .CreateLogger();

@SathishN thanks for the follow-up!

I wonder - would setting:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

cover it? It’d be interesting to hear if this fixes the issue.

We can’t make this change globally because it would break those already depending on the use of log paths relative to the current directory. If cding works, we might be able to do something in https://github.com/serilogweb/classic.

Cheers!

@navnit-kumar, thanks for your help.

I managed to get it working. My problem for two things:

  1. Missing <add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" /> in the config file
  2. Wrong working directory. I was expecting the logger’s working directory to be relative to the webapp’s DLL location, but it was relative to C:\Program Files\IIS Express… because I was hosting in NancyFx. I tried to override the NancyFx IRootPathProvider as in the following solution but it did not work for the logger…

Look like I’m the only person having this issue,

I looked into the code https://github.com/serilog/serilog/blob/dev/src/Serilog.FullNetFx/Sinks/RollingFile/TemplatedPathRoller.cs#L47

Following code used to resolving the path,

var directory = Path.GetDirectoryName(pathTemplate);
if (string.IsNullOrEmpty(directory))
{
#if ASPNETCORE50
    directory = Directory.GetCurrentDirectory();
#else
    directory = Environment.CurrentDirectory;
#endif
}
directory = Path.GetFullPath(directory);

Instead of Environment.CurrentDirectory, using AppDomain.CurrentDomain.BaseDirectory, should fix this problem.

I’m running the web app on _IIS Express, so the current directory is _C:\Program Files (x86)\IIS Express, not my work project working directory!!

any thoughts? If your are good, I could submit a pull request with my fix!