在Azure应用服务的Azure日志流中显示重复的日志条目

时间:2019-06-27 08:43:32

标签: .net azure-web-sites

我有一个.Net Core 2.2 Web应用程序,它使用开箱即用的ILogger记录有关该应用程序的信息。在本地调试时,信息将按预期记录在调试控制台中。但是,当将应用程序作为应用程序服务部署到Azure时,在Azure LogStream中查看时,日志条目会重复。

在Azure门户上,安装了Asp.Net Core日志记录扩展(v 2.2.0)

这是在我的Web应用程序中完成日志记录的代码(c#)

public class MyService : BackgroundService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        _logger.Log(LogLevel.Information, "The service task is starting.");
        var count = 0;
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.Log(LogLevel.Information, $"The service task is doing background work. count is: {count}. Hashcode is: {_logger.GetHashCode()} on thread: {Thread.CurrentThread.ManagedThreadId}");

            // some background work

            count++;

            Task.Delay(60000, stoppingToken).GetAwaiter().GetResult();
        }

        _logger.Log(LogLevel.Information, "The service task is terminating.");

        return (Task.FromResult<object>(null));
    }
}

在program.cs文件中这样配置日志记录

    public static IWebHost BuildWebHost(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .CaptureStartupErrors(true)
            .UseSetting(WebHostDefaults.DetailedErrorsKey, "true")
            .ConfigureLogging((logging) =>
                {
                    logging.AddAzureWebAppDiagnostics();
                })
            .UseStartup<Startup>()
            .Build();
    }

这是nuget软件包中的后台服务:microsoft.extensions.hosting.abstractions \ 2.1.0

namespace Microsoft.Extensions.Hosting
{
    public abstract class BackgroundService : IHostedService, IDisposable
    {
        protected BackgroundService();
        public virtual void Dispose();
        // more methods
    }
}

将MyService添加到服务集合

public class Startup
{
    private readonly ILoggerFactory _loggerFactory;
    private readonly ILogger _logger;
    private readonly ILogger _startupLogger;

    public Startup(IConfiguration configuration, ILoggerFactory loggerFactory)
    {
        Configuration = configuration;
        _loggerFactory = loggerFactory;
        _logger = _loggerFactory.CreateLogger("GlobalException");
        _startupLogger = _loggerFactory.CreateLogger(typeof(Startup));
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
       _startupLogger.LogInformation($"Adding common services");

       services.AddCommonServices(Configuration, _logger);
    }
}

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddCommonServices(this IServiceCollection services, IConfiguration configuration, ILogger logger)
    {
        services.AddMvc(options =>
        {
            options.Filters.Add(new ExceptionFilter(logger));
        })

        services.AddSingleton<IHostedService, MyService>();

        return services;
    }
}

在Azure LogStream中,我期望每个任务周期只有一行输出,如下所示: 2019-06-27 08:36:25.488 +00:00 [Information] MyService:延迟的后台服务任务正在执行后台工作。计数为:3.哈希码为:61705107,线程:1

但是我得到了三个这样的副本:

2019-06-27 08:36:25.488 +00:00 [Information] MyService:服务任务正在做后台工作。计数为:3.哈希码为:61705107,线程:1

2019-06-27 08:36:25.482 +00:00 [Information] MyService:服务任务正在做后台工作。计数为:3.哈希码为:61705107,线程:1

2019-06-27 08:36:25.224 +00:00 [Information] MyService:服务任务正在做后台工作。计数为:3.哈希码为:61705107,线程:1

0 个答案:

没有答案