Reputation: 491
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.
Upvotes: 13
Views: 28240
Reputation: 19
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.
Upvotes: 1
Reputation: 20106
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.
Upvotes: 1
Reputation: 141442
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/
Upvotes: 6