Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 806
ashilon Avatar asked Sep 22 '16 10:09

ashilon


People also ask

Where do .NET Core logs go?

You have literally declared that logs will go to four potential destinations: App Insights, the console, the debug output, and the event viewer.

How do I open the console in .NET Core?

Regardless, you can still see the console output in the Output pane in Visual Studio. There's a drop down there called "Show output from" and one of the choices is "ASP.NET Core Web server". Select this and you'll see all the console output.

Where can I find ASP.NET logs?

ASP.NET uses IIS logging, so it's really an IIS question. Though there is some detailed info in Event Viewer for some types of events. In IIS6 (and prior), this is located in %SystemRoot%\system32\logfiles , and in IIS7, this is located in %SystemDrive%\inetpub\logs\LogFiles .

Where is the ASP.NET Core module stdout log?

\%home%\LogFiles\stdout .


1 Answers

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.

like image 97
Nicholas Blumhardt Avatar answered Dec 24 '22 00:12

Nicholas Blumhardt