[英]Serilog does not write log to file while using InProcess hosting model in ASP.NET Core 2.2
[英]Serilog in ASP.NET Core Windows Service cannot write file as Local System
我正在运行ASP.NET Core Web服务器作为Windows服务,并使用Serilog登录到%PROGRAMDATA%中的文件。 当我以本地系统运行服务时,不会记录任何内容。
我在Windows 10上使用.Net Core 2.2。我正在通过触发我的服务中的错误来测试,该错误在错误级别写入日志事件。 我已经尝试将该服务作为我自己的管理帐户运行,并且日志记录工作正常; 只有在作为本地系统运行时才会出现此问题。
我有其他Windows服务使用.Net Framework作为本地系统运行并且没有问题使用Serilog记录到%PROGRAMDATA%,但这是我第一次在.Net Core上试用它。 我可以使用Directory.CreateDirectory和File.AppendText手动创建和写入服务中的日志文件,并在作为本地系统运行时工作,但Serilog日志记录不能。
这是我的Program.Main:
public static async Task Main(string[] args)
{
var isService = !(Debugger.IsAttached || args.Contains("--console"));
if (isService)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
Directory.SetCurrentDirectory(pathToContentRoot);
}
var host = WebHost.CreateDefaultBuilder(args.Where(arg => arg != "--console").ToArray())
.UseStartup<Startup>()
.UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
.ReadFrom.Configuration(hostingContext.Configuration)
.Enrich.FromLogContext())
.Build();
// additional async initialization omitted
if (isService)
{
host.RunAsService();
}
else
{
host.Run();
}
}
这是我的appsettings.json的Serilog部分:
"Serilog": {
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "%PROGRAMDATA%/foo/bar baz/logs/qux.log",
"fileSizeLimitBytes": 1048576,
"rollOnFileSizeLimit": "true",
"retainedFileCountLimit": 99,
"flushToDiskInterval": "00:00:01",
"outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}"
}
},
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj} [{SourceContext}]{NewLine}{Exception}"
}
}
]
}
当服务作为本地系统运行时,我希望将日志记录写入%PROGRAMDATA%中的文件,但没有任何反应。 当服务作为任何其他管理帐户运行时,日志写入没有问题。
如果我不想念 - 理解它是有趣的帐户系统权限:
“本地管理员”的本地系统帐户与管理员帐户相同。
如果您有域管理员,并且您有本地管理员。 两者都具有相同的功能。
如果您是域的一部分,通常不希望以域管理员身份登录计算机。
如果可以,您始终希望使用本地管理员帐户。 这背后的原因是您的计算机上可能有病毒,您以域管理员身份登录,您刚刚打开整个网络中的病毒之门
如果你需要写入%PROGRAMDATA%,那么你应该给予许可并使用这样的https://stackoverflow.com/a/30792263/914284
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.