question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AspNetCoreServer and Duplicate Logging

See original GitHub issue

We’re currently experiencing an issue with Amazon.Lambda.AspNetCoreServer and Logging.

We’re hosting an asp.net core web site in Lambda. The web site works however I’m getting duplicates in the logs in CloudWatch and I haven’t been able to figure out the cause. I’ve removed all the logging setup identified in Amazon.Lambda.Logging.AspNetCore and the behaviour is exactly the same regardless. App settings also has no logging configuration.

Here is an example of what I’m seeing in the log:

  • 2020-06-20T20:50:56.705-04:00 START RequestId: fdbd1980-8263-4b5f-8bfc-ef8ec43e0085 Version: $LATEST
  • 2020-06-20T20:50:56.705-04:00 [40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
  • 2020-06-20T20:50:56.707-04:00 Application started. Press Ctrl+C to shut down.
  • 2020-06-20T20:50:56.708-04:00 [Information] Microsoft.Hosting.Lifetime: Application started. Press Ctrl+C to shut down.
  • 2020-06-20T20:50:56.708-04:00 [40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
  • 2020-06-20T20:50:56.708-04:00 Hosting environment: Production
  • 2020-06-20T20:50:56.708-04:00 [40m[32minfo[39m[22m[49m: Microsoft.Hosting.Lifetime[0]
  • 2020-06-20T20:50:56.708-04:00 Content root path: /var/task
  • 2020-06-20T20:50:56.708-04:00 [Information] Microsoft.Hosting.Lifetime: Hosting environment: Production
  • 2020-06-20T20:50:56.708-04:00 [Information] Microsoft.Hosting.Lifetime: Content root path: /var/task
  • 2020-06-20T20:50:56.880-04:00 [40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Hosting.Diagnostics[1]
  • 2020-06-20T20:50:57.456-04:00 [Information] Project.Classes.GetFileHandler: Started Project.Classes.GetFileHandler
  • 2020-06-20T20:50:57.456-04:00 Started Project.Classes.GetFileHandler

Below is a simplified version of my code.

    public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
    {
        protected override void Init(IWebHostBuilder builder)
        {
            RegisterResponseContentEncodingForContentType("application/pdf",
                    ResponseContentEncoding.Base64);
            builder
                .UseStartup<Startup>();
        }

        protected override void Init(IHostBuilder builder)
        {
        }
    }
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            IConfigurationSection appSettings = Configuration.GetSection("AppSettings");

            services.Configure<AppSettings>(appSettings);
            services.AddAWSService<IAmazonS3>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
			
            app.MapWhen(
                context => context.Request.Path.ToString().EndsWith("file.ashx"),
                appBranch => {
                    // ... optionally add more middleware to this branch
                    appBranch.UseGetFileHandler();
                });
        }
    }
    public class GetFileHandler
    {
        public GetFileHandler(RequestDelegate next)
        { }

        public async Task Invoke(HttpContext context, ILogger<GetFileHandler> logger)
        {
            logger.LogInformation("Started " + this.GetType());
        }
    }

Thanks for any help.

Regards Artin

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ayongbannayancommented, Aug 7, 2020

I resolved it by using the work around I specified. Specifically clearing the providers set by the library and resetting them myself. I don’t know if the underlying issue has been resolved.

2reactions
ayongbannayancommented, Jun 23, 2020

I’ve been able to work around the issue by doing the following in the LambdaEntryPoint class:

        protected override void Init(IWebHostBuilder builder)
        {
            RegisterResponseContentEncodingForContentType("application/pdf",
                    ResponseContentEncoding.Base64);
            builder
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.ClearProviders();
                    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                    logging.AddLambdaLogger(hostingContext.Configuration, "Logging");
                })
                .UseStartup<Startup>();
        }

Haven’t been able to figure out what in the base class would be causing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serilog duplicate log statements - asp.net core
Console in Program.cs , output to console duplicate cause you have two WriteTo.Console , One from appSetting.json , one from builder.
Read more >
Logging in .NET Core and ASP.NET Core
Logging set to Logging:Console:LogLevel:Microsoft:Information . Navigation to the Privacy page: Console Copy. info: Microsoft.AspNetCore.
Read more >
Logging and diagnostics in Kestrel
Logging to emit log information. Kestrel employs the use of multiple categories which allows you to be selective on which logs you listen...
Read more >
c# - Simple way to prevent duplicate logging of errors
I've been tasked with updating our current error logging system to prevent the logging of similar errors (from a single client program) that ......
Read more >
Serilog.Extensions.Logging 3.1.1-dev-10338
Low-level Serilog provider for Microsoft.Extensions.Logging. ... then a potentially very large block of text would be duplicated within your log event!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found