2

我创建了一个 C# .net5.0 控制台应用程序,并且在测试期间 Serilog 一直在正常工作,登录到控制台和文件(相同的文件夹;path="log.txt")。但是,当我在我们服务器上的应用程序上运行时,控制台和文件日志接收器都不起作用!我现在假设问题不在于接收器本身,而是 Serilog 没有实际工作。

我试过启用自我日志:

Serilog.Debugging.SelfLog.Enable(msg =>
    Console.WriteLine(msg)
);

但即使在我的开发环境的调试器中运行,Console.WriteLine(msg)也不会调用该行!

appsettings.json的如下:

{
"Serilog": {
    "MinimumLevel": {
        "Default": "Debug",
        "Override": {
            "Microsoft": "Information",
            "System": "Information"
        }
    },
    "WriteTo": [
        {
            "Name": "Console",
            "Args": {
                "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
                "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {NewLine}{Exception}"
            }
        },
        {
            "Name": "File",
            "Args": {
                "path": "log.txt",
                "rollingInterval": "Infinite",
                "outputTemplate": "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
                "shared": false
            }
        }
    ],
    "Enrich": [ "FromLogContext" ]
},

"Database": {
    "Server": "(local)",
    "Database": "ActivbaseLive"
},

"Email": {
    "SmtpHost": "localhost",
    "SmtpPort": 25,
    "SmtpSecurityOption": "None",
    "SmtpUsername": null,
    "SmtpPassword": null,
    "SmtpSender": "\"Activbase Learning Platform\" <noreply@activbase.net>"
}
}

我尝试过绝对路径(在 中使用双反斜杠appsettings.json)。我已经尝试预先创建日志文件(例如log.txtlog200428.txt)并将权限设置为“所有人完全控制”,但这些更改都没有解决问题,并且他们没有解释为什么控制台接收器也不写入。

以下是在启动期间如何配置 Serilog,这是我怀疑问题所在(即使它在开发环境中工作):

return Host.CreateDefaultBuilder()
    .ConfigureLogging(logging =>
    {
        logging.ClearProviders();
    })
    .UseSerilog((hostContext, loggerConfiguration) =>
    {
        loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
    })
    .ConfigureAppConfiguration((hostContext, builder) =>
    {
        builder.AddEnvironmentVariables();
    })
    .ConfigureServices(services =>
    {
        services.AddHostedService<Worker>();
        ...
    });
}

任何想法为什么 Serilog 不在生产中工作?

4

2 回答 2

0

对于在 IIS 中运行的我的 api 应用程序:我必须为 IIS_IUSRS 的日志文件夹分配以下权限。我不需要绝对路径!

在此处输入图像描述

于 2022-01-30T20:51:50.780 回答
0

您提供的路径应该是绝对的。像这样的一些事情:

"path": "E:/wwwroot/QA/BackgroundWorkerService/Logs/logFile_.log"

即使我有同样的问题,上述修复对我来说也很好......

于 2021-11-18T12:42:51.057 回答