10

I have an ASP.NET Core 2 web app I'm deploying to production, and need to enable logs to troubleshoot an error. I can't find anywhere in the docs as to the schema in the logger section of appsettings.json. Here's what I have there:

"Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information" }
    ,"Console": { "IncludeScopes": "true" }
  }

When I run the exe from the console, the log info is shown in the console, but entity framework is also logging so I can't see my own messages as the amount of output displayed is limited. Thus I need to output the logs to a file, say C:\temp\my.log. How can I configure ASP.NET Core 2 to do this?

Thanks in advance.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
Fernando Gómez
  • 464
  • 1
  • 5
  • 19
  • 1
    I think you have to set `stdoutLogEnabled` to `true` and `stdoutLogFile` to some valid folder path in the deployed application's web.config – Camilo Terevinto Aug 16 '18 at 22:55
  • I believe you need to add a third party logger to write to files. One of the easiest to add is [Serilog](https://github.com/serilog/serilog-aspnetcore). It has a sink to [write to a file](https://github.com/serilog/serilog-sinks-file) – Simply Ged Aug 17 '18 at 01:02

3 Answers3

5

Thus I need to output the logs to a file, say C:\temp\my.log. How can I configure ASP.NET Core 2 to do this?

Serilog has a package for that.

dotnet add package Serilog.Extensions.Logging.File

We configure it like this.

public void ConfigureServices(IServiceCollection services)
{
    services.AddLogging();
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 
{
    loggerFactory.AddFile("C:\\temp\\my.log");
}

See also: https://nblumhardt.com/2016/10/aspnet-core-file-logger/

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
1

This issue seems has the same requirement as yours.

You could also try to use NLog to output the logs to a file. It could also determine the layout of logs in its config file.

After installing,in startup.cs, set logs folder:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        GlobalDiagnosticsContext.Set("configDir", "C:\\git\\damienbod\\AspNetCoreNlog\\Logs");
        GlobalDiagnosticsContext.Set("connectionString", Configuration.GetConnectionString("NLogDb"));

        loggerFactory.AddNLog();

        app.UseMvc();
    }

Then in nlog.config,set fileName:

 <target xsi:type="File" name="allfile" fileName="${gdc:item=configDir}\nlog-all.log"/>

Here is an example you could refer to.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Ryan
  • 19,118
  • 10
  • 37
  • 53
  • 2
    Thanks! I ended up using NLog as you suggested. Can't believe one needs a third party tool for this, I would have thought this was a very common scenario. – Fernando Gómez Aug 17 '18 at 21:45
1

The alternative way it's using middleware like in this RequestResponseLoggingMiddleware.cs file

Please read comments on gist because code the has some problems. Comments about body seek position are important.

Setting up the web.config param stdout like stdoutLogEnabled="true" and stdoutLogFile=".\logs\stdout"

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
  <location path="." inheritInChildApplications="false">
   <system.webServer>
    <handlers>
     <add name="aspNetCore" path="*" verb="*" 
          modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" 
                arguments=".\ReportViewer.dll" 
                stdoutLogEnabled="true" 
                stdoutLogFile=".\logs\stdout" />
   </system.webServer>
 </location>
</configuration>

And you should create two folders logs and logs\stdout in the project directory.

surfmuggle
  • 5,527
  • 7
  • 48
  • 77