Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Serilog:azure webjob日志记录不';在azure中托管时似乎不起作用?_Azure_Logging_Serilog_Azure Webjobssdk_Azure Webjobs Continuous - Fatal编程技术网

Serilog:azure webjob日志记录不';在azure中托管时似乎不起作用?

Serilog:azure webjob日志记录不';在azure中托管时似乎不起作用?,azure,logging,serilog,azure-webjobssdk,azure-webjobs-continuous,Azure,Logging,Serilog,Azure Webjobssdk,Azure Webjobs Continuous,我有azure webjob sdk(v3.0.3)应用程序,该应用程序已配置为使用serilog进行日志记录。 当我在系统中本地运行应用程序时,日志似乎起作用。以下是配置: static void Main(string[] args) { try { var builder = new HostBuilder() .ConfigureAppC

我有azure webjob sdk(v3.0.3)应用程序,该应用程序已配置为使用serilog进行日志记录。 当我在系统中本地运行应用程序时,日志似乎起作用。以下是配置:

    static void Main(string[] args)
        {
            try
            {
                var builder = new HostBuilder()
                        .ConfigureAppConfiguration(SetupConfiguration)
                        .ConfigureLogging(SetupLogging)
                        .ConfigureServices(SetupServices)
                        .ConfigureWebJobs(webJobConfiguration =>
                        {
                            webJobConfiguration.AddTimers();
                            webJobConfiguration.AddAzureStorageCoreServices(); //this is to store logs in azure storage 
                        })
                        .UseSerilog()
                    .Build();
                builder.Run();
            }
}
SetupConfiguration的代码如下所示:

  private static void SetupConfiguration(HostBuilderContext hostingContext, IConfigurationBuilder builder)
        {
            var env = hostingContext.HostingEnvironment;
            _configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();
        }
设置服务的代码:

private static void SetupServices(HostBuilderContext hostingContext, IServiceCollection serviceCollection)
    {
        serviceCollection.AddSingleton<IConfiguration>(_configuration);
        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(_configuration)
            .CreateLogger();
        _logger = serviceCollection.BuildServiceProvider().GetRequiredService<ILoggerFactory>().CreateLogger("test");
    }
在我的TimerTrigger方法中,我使用的是记录器:

[Singleton]
        public async static Task Trigger([TimerTrigger("%Job%")]TimerInfo myTimer)
        {           
          _logger.LogInformation($"From Trigger {DateTime.UtcNow.ToString()}");

        }
在appSettings.json中,serilog的配置如下:

"Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
        {
            "Name": "RollingFile",
            "Args": {
                "pathFormat": ".\\Log\\log-{Date}.txt",
                "retainedFileCountLimit": 7,
                "fileSizeLimitBytes": 5000000,
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {EventId} [{Level}] [{Properties}] {Message}{NewLine}{Exception}"
            }
        }
    ]
}
当我在本地运行应用程序时,会创建文件夹“Log”和日志文件。但是当我发布webjob时,webjob的“app_data”文件夹中不会创建“Log”文件夹或日志文件。有人能帮我弄清楚如何配置serilog,使其与webjobs一起工作吗

以下是使用的nuget软件包:

如果要在
WebJob
中使用
serilog
,则需要安装此软件包
serilog.Extensions.WebJobs
。然后,在配置
serilog
之后,您就可以使用它了

必须插入ILogger而不是使用全局日志记录器,否则日志消息将不会写入Microsoft Azure WebJobs仪表板。

关于如何配置和使用
serilog
的详细说明,您可以参考以下内容


希望这能帮助您,如果您还有其他问题,请告诉我。

注意,这不再适用于WebJobs V3:
"Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
        {
            "Name": "RollingFile",
            "Args": {
                "pathFormat": ".\\Log\\log-{Date}.txt",
                "retainedFileCountLimit": 7,
                "fileSizeLimitBytes": 5000000,
                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} {EventId} [{Level}] [{Properties}] {Message}{NewLine}{Exception}"
            }
        }
    ]
}