指定 Serilog 滚动文件路径的目录 [英] Specify directory for Serilog rolling file path
问题描述
考虑这个 app.config appSetting
条目:
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();
这是在 Windows 服务应用程序中.日志文件在此处结束:
This is in a Windows service app. The log file ends up here:
C:\Windows\SysWOW64
显然,我们更愿意将日志文件放在存放此服务的 .exe 的同一目录中(客户不希望我们向 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?
我们需要其中的 ReadFrom.AppSettings,以便客户可以根据需要在 app.config 中提供 serilog 设置.
We need the ReadFrom.AppSettings in there so that the customer can supply serilog settings in the app.config, as necessary.
ReadFrom.AppSettings 完成后,有什么方法可以更改用于日志文件的目录吗?
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" />
(还有{Date}在哪里,可以放在文件路径中,记录?)
(And where is {Date}, which can be put in the file path, documented?)
推荐答案
服务写入日志的最佳位置是 %PROGRAMDATA%
,默认情况下,它位于 C:\ProgramData\
.
The best place for services to write their logs is %PROGRAMDATA%
which, by default, is in C:\ProgramData\
.
试试:
<add key="serilog:write-to:RollingFile.pathFormat"
value="%PROGRAMDATA%\ServerService\Logs\log-{Date}.txt" />
(Program Files
通常被认为是只读的,在这里写东西会导致卸载时意外遗留一些奇怪的东西.)
(Program Files
is usually considered to be read-only, and writing stuff here will lead to oddities being left behind unexpectedly during uninstall.)
这篇关于指定 Serilog 滚动文件路径的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!