Webjob日志记录似乎不起作用

Webjob日志记录似乎不起作用

本文介绍了Serilog:在Azure中托管时,Azure Webjob日志记录似乎不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure Webjob SDK(v3.0.3)应用程序,该应用程序已配置为使用serilog进行日志记录.当我在系统中本地运行应用程序时,日志似乎可以正常工作.下面是配置:

I have azure webjob sdk (v3.0.3) app which has been configured to use serilog for logging.The log seems to work when I run the app locally in my system. Below is the configuration:

    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的代码如下:

The code for SetupConfiguration is below:

  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");
    }

日志记录设置如下:

  private static void SetupLogging(HostBuilderContext hostingContext, ILoggingBuilder loggingBuilder)
        {
            loggingBuilder.SetMinimumLevel(LogLevel.Information);
            loggingBuilder.AddConsole();
            loggingBuilder.AddDebug();
            loggingBuilder.AddSerilog(dispose: true);
        }

在我的TimerTrigger方法中,我使用记录器:

In my TimerTrigger method I'm using the logger:

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

        }

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

In appSettings.json, serilog is configured as follows:

"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兼容?

the folder "Log" and the log files get created when i run the app locally. But when I publish the webjob, the "Log" folder or the log file is not created in the "app_data" folder of webjob. Can anyone help me figureout how to configure serilog to make it work with webjobs?

以下是使用的nuget软件包:

Following are the nuget packages used:

推荐答案

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

If you want to use serilog in WebJob , you need to install this package Serilog.Extensions.WebJobs. Then after configuring the serilog, you would be able to use it.

您必须注入ILogger而不是使用全局Log.Logger,否则日志消息将不会写入Microsoft Azure WebJobs仪表板.

关于如何配置和使用serilog的详细说明,可以参考这个文档.

About the detailed description about how to configure and use serilog, you could refer to this doc.

希望这可以为您提供帮助,如果您还有其他问题,请告诉我.

Hope this could help you, if you still have other questions, please let me know.

这篇关于Serilog:在Azure中托管时,Azure Webjob日志记录似乎不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:26