避免在带有Seri​​log的AWS Lambda上使用netcore2.0两次记录 [英] Avoid logging twice using netcore2.0 on AWS Lambda with Serilog

查看:106
本文介绍了避免在带有Seri​​log的AWS Lambda上使用netcore2.0两次记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将netcore项目升级到2.0之后,当我的应用程序在使用Serilog框架的AWS Lambda上运行时,我会看到重复的日志.请在下面查看我的设置:

After upgrading my netcore project to 2.0, i see double logs when my application is running on AWS Lambda, which utilizes the Serilog framework. Please see my setup below:

    public void ConfigureServices(IServiceCollection services)
    {
        ...

        // Setup logging
        Serilog.Debugging.SelfLog.Enable(Console.Error);
        var logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Enrich.FromLogContext();

        if (GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "").Equals("local"))
            logger.WriteTo.Console();
        else
            logger.WriteTo.Console(new JsonFormatter());

        Log.Logger = logger.CreateLogger();

        ...
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddSerilog();
        ...
    }

关于为什么突然出现这种情况的任何想法? Lambda捕获所有控制台日志语句并将其插入到cloudwatch中,但是现在两次并以两种不同的格式插入.例如

Any ideas as to why this suddenly might be the case? Lambda grabs any console log statements and inserts into cloudwatch, but now twice and in two different formats. Eg.

[信息] PodioDataCollector.Controllers.CustomerController: 验证API密钥

[Information] PodioDataCollector.Controllers.CustomerController: Validating API Key

{ "Timestamp":"2018-02-14T08:12:54.7921006 + 00:00", 级别":信息", "MessageTemplate":正在验证API密钥", 特性": { "SourceContext":"...", "ActionId":"...", "ActionName":"...", "RequestId":"...", "RequestPath":"...", "CorrelationId":"..." }

{ "Timestamp": "2018-02-14T08:12:54.7921006+00:00", "Level": "Information", "MessageTemplate": "Validating API Key", "Properties": { "SourceContext": "...", "ActionId": "...", "ActionName": "...", "RequestId": "...", "RequestPath": "...", "CorrelationId": "..." } }

我只希望日志消息以json格式格式化.这是在我升级到netcore2.0时开始的

I only expected the log message formatted in json. This started when i upgraded to netcore2.0

预先感谢

推荐答案

在Core 2.0中,日志记录实现和Serilog提供程序都发生了一些变化.

The logging implementation, and Serilog provider, have both changed a little in Core 2.0.

您将需要: https://github.com/serilog/serilog-aspnetcore 代替 Serilog.Extensions.Logging (设置说明位于README上).

You will need: https://github.com/serilog/serilog-aspnetcore instead of Serilog.Extensions.Logging (setup instructions are on the README).

这篇关于避免在带有Seri​​log的AWS Lambda上使用netcore2.0两次记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆