我有一个使用以下方法安装为服务的ASP.NET Core 8 Web API:

问题描述 投票:0回答:0

builder.Host.UseSerilog((ctx, cfg) => cfg.ReadFrom.Configuration(ctx.Configuration));

appsettings.json

{
    "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
    },
    "Serilog": {
        "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
        "MinimumLevel": "Debug",
        "WriteTo": [
          {
            "Name": "Console"
          },
          {
            "Name": "File",
            "Args": {
              "path": "D:\\deployments\\logs\\myapp\\log-.txt",
              "rollingInterval": "Day"
            }
          }
        ],
        "Enrich": [ "FromLogContext", "WithMachineName" ],
        "Properties": {
          "ApplicationName": "Your ASP.NET Core App"
        }
    },
    "AllowedHosts": "*"
}
中我有以下配置:

sc

应用程序在Windows Server 2022标准计算机上安装为服务,并且该服务在本地系统帐户下运行。

如果我使用
dotnet publish --configuration Release --output publish --runtime win-x64 --self-contained=true

命令直接在服务器上安装了该应用程序,或者如果直接启动EXE,我可以看到正在生成的日志,但是如果我使用上面的ProcessStartInfo方法安装它,我将看不到任何日志,在Serilog selbogs中,我可以看到以下异常。
通过Seriloglogger进行编写事件:System.IO.FilenotFoundException:无法加载文件或汇编'System.diarostics.diarostics.diagnosticsource,版本= 9.0.0.0.0,culture = Neutral,public KeyperKeyToken = CC7B13FFCD2DDDDDD51'。该系统找不到指定的文件。
文件名:'system.diagnostics.diagnosticsource,版本= 9.0.0.0,文化=中性,publickeytoken = cc7b13ffcd2dddddddd51'
在serilog.extensions.logging.seriloglogger.preparewrite [tstate](logeventlevel级别,EventID EventID,TSTATE状态,异常例外,Func3 Formatter))
在serilog.extensions.logging.seriloglogger.log [tstate](loglevel loglevel,Eventid Event,tState State,exception exception,func`3 formatter)

应用程序是使用以下命令

构建的
Program.cs

I还尝试在

Program.cs

中直接设置Serilog配置
ILogger<MyClass>

您尝试配置用于调试的Serilog:

Log.Logger = new LoggerConfiguration()
     .MinimumLevel.Debug()
     .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
     .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
     .WriteTo.Console()
     .WriteTo.File(
         path: Path.Combine("D:\\deployments\\logs\\myapp", "log-.txt"),
         rollingInterval: RollingInterval.Day,
         shared: true // Important for services
     )
     .Enrich.FromLogContext()
     .Enrich.WithMachineName()
     .Enrich.WithProperty("ApplicationName", "Your ASP.NET Core App")
     .CreateLogger();

Log.Information("Environment:" + builder.Environment.EnvironmentName);

builder.Host.UseWindowsService();
builder.Host.UseSerilog(Log.Logger);

启用内部日志记录并显示与您的接收器有关的任何问题,例如文件接收器。它帮助我解决了SEQ接收器的身份验证问题。
问题可能与文件访问有关。

尝试一下,并检查控制台输出是否有任何错误!
	
c# asp.net-core-webapi windows-services .net-8.0 serilog
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.