question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
nblumhardtcommented, Jun 18, 2015

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!

6reactions
codecyphercommented, Jan 4, 2018

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();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why IIS doesn't handle relative path the same way IIS ...
Indeed, IIS Express seem to run the app from the root while a web deploy on IIS will, by defaut, create a application...
Read more >
IIS Express on VS2022 not loading CSS,images and other ...
IIS Express on VS2022 not loading CSS,images and other static content This is happening when I'm trying to access a ".aspx" page in...
Read more >
Setting up Serilog in ASP.NET Core - Detailed Beginner ...
Here I have specified a relative path that will create folder serilogs in the application folder and write to the file AppLogs.log in...
Read more >
Log creation and redirection with the ASP.NET Core Module
The following sample aspNetCore element configures stdout logging at the relative path .\log\ . Confirm that the AppPool user identity has ...
Read more >
IIS express doesn't show index.cshtml in one folder but in ...
Coding example for the question IIS express doesn't show index.cshtml in one folder but in other it shows-Asp.Net-Mvc.
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