Specify the directory to scroll the Serilog file

Consider this app.config entry appSetting

:

<add key="serilog:write-to:RollingFile.pathFormat"
 value="ServerServiceApp-{Date}.log" />

      

This is done when the application starts:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .Enrich.WithThreadId()
    .CreateLogger();

      

This application is for Windows. The log file ends here:

C:\Windows\SysWOW64

      

Obviously, wed rather has a log file in the same directory that hosts this .exe service (clients don't want us to write files to SysWOW64). But how?

We need ReadFrom.AppSettings there so that the client can set the serilog parameters in app.config if needed.

Is there a way to change the directory used for the log file after ReadFrom.AppSettings is executed?

It would be great if we could say something like:

<add key="serilog:write-to:RollingFile.pathFormat"
 value="{ApDomainBaseDirectory}\ServerServiceApp-{Date}.log" />

      

(And where is {Date}, which can be placed in the file path, documented?)

+3


source to share


1 answer


The best place for services to write their logs is %PROGRAMDATA%

, which is in the default C:\ProgramData\

.

Try:



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

      

(It is Program Files

generally considered read-only, and writing stuff here will lead to surprises that are left unexpectedly during deletion.)

+5


source







All Articles