ashilon
ashilon

Reputation: 1941

Where are the console logs in an asp.net core application hosted by IIS

I've added logging to an asp.net core application, using the excellent Serilog library. I have added sinks for both Sql Server and Literate console.
The sql server sink works perfectly of course, but, well this is a little embarrassing, I have no clue how to display the logs in a console window.
This is what ive tried:

  1. In the published directory I have run the command dotnet run but got an error saying that the project.json does not exist. This of course works if I run the command on my dev machine where that file is located. The logs are displayed the console that is opened.
  2. I've also tried to run the exe directly, and it was saying that it is listening on: http://localhost:5000, but no logging was displayed in console window.

Here's the code:

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();

        ConfigureLogging();
    }

    private void ConfigureLogging()
    {
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Verbose()
            .WriteTo.LiterateConsole(LogEventLevel.Verbose)
            .WriteTo.RollingFile(".\\Logs\\log-{Date}.txt")
            .WriteTo.MSSqlServer(Configuration.GetConnectionString("Logging"), "Logs"/*, columnOptions: new ColumnOptions()*/)
            .CreateLogger();
    }

    public IConfigurationRoot Configuration { get; }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
    {
        app.Use(async (context, next) =>
        {
            await next();

            // If there's no available file and the request doesn't contain an extension, we're probably trying to access a page.
            // Rewrite request to use app root
            if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/"; // Put your Angular root page here 
                await next();
            }
        });

        loggerFactory
            .AddSerilog();

        // Ensure any buffered events are sent at shutdown
        appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseDefaultFiles();
        app.UseStaticFiles();

        //app.UseCors(builder => builder.AllowAnyOrigin());
        app.UseMvc();



        //app.UseHangfireDashboard();             // Will be available under http://localhost:5000/hangfire
        //app.UseHangfireServer();
    }

How do I display the logs in the console window?

Upvotes: 8

Views: 5872

Answers (1)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31787

Under IIS the console logs aren't collected. You'll need to use one of the other sinks for an IIS hosted app, such as the rolling file you've configured.

Upvotes: 8

Related Questions