Serilog 未在生产服务器上创建日志文件

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

我创建了一个 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\" <[email protected]>"
}
}

我尝试过绝对路径(在

appsettings.json
中使用双反斜杠)。 我尝试预先创建日志文件(例如
log.txt
log200428.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 无法在生产环境中运行吗?

serilog serilog-sinks-file
3个回答
4
投票

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

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

即使我遇到了同样的问题,上述修复对我来说效果很好......


2
投票

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


0
投票

对于我本地来说,我没有遇到问题。但是,当我的应用程序作为 Windows 服务运行时,相对路径不再起作用。我通过添加以下代码解决了这个问题。

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
© www.soinside.com 2019 - 2024. All rights reserved.