Samy Sammour
Samy Sammour

Reputation: 2465

serilog appSettings write to file in the same server path asp.net mvc

I have a serilog code and I want to move the configuration to the web.config file.

everything works fine but the problem is with the path of the file:

var logFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"EnBW.DokumentErfassung.Web-{Environment.MachineName}.log");
    Log.Logger = new LoggerConfiguration()
                        .ReadFrom.AppSettings();

I want to write the new file in the same server path, the web.config code is:

<add key="serilog:using:RollingFile" value="Serilog.Sinks.RollingFile" />
    <add key="serilog:write-to:RollingFile.pathFormat" value="log123-{Date}.txt" />

but the files saved has been saved in C:/program file/ .....

I want it to be saved in the same Domain path. what should I do?

Upvotes: 5

Views: 6041

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31832

The app settings provider supports environment variables, so in Global.asax.cs before configuring the logger:

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

And use %BASEDIR% in config:

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

Upvotes: 12

Related Questions