Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
Samy Sammour Avatar asked Dec 19 '22 12:12

Samy Sammour


1 Answers

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" />
like image 57
Nicholas Blumhardt Avatar answered Dec 29 '22 10:12

Nicholas Blumhardt