Specify directory for Serilog rolling file path

北战南征 提交于 2019-12-01 05:04:40

问题


Consider this app.config appSetting entry:

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

This is done at app startup:

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

This is in a Windows service app. The log file ends up here:

C:\Windows\SysWOW64

Clearly, we’d rather have the log file end up in the same directory that houses this service’s .exe (customers don’t want us writing stuff to SysWOW64). But how?

We need the ReadFrom.AppSettings in there so that the customer can supply serilog settings in the app.config, as necessary.

Is there some way to change the directory used for the log file after the ReadFrom.AppSettings has been done?

Would be awesome 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 put in the file path, documented?)


回答1:


The best place for services to write their logs is %PROGRAMDATA% which, by default, is in C:\ProgramData\.

Try:

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

(Program Files is usually considered to be read-only, and writing stuff here will lead to oddities being left behind unexpectedly during uninstall.)



来源:https://stackoverflow.com/questions/30765731/specify-directory-for-serilog-rolling-file-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!