I have ASP.NET Core web api project. Now I want to log the errors and events. I previously used ELMAH in my project but it seems that elmah is not available for ASP.NET Core. I referred to this Official link for configuring the default logging service provided by Microsoft. I see nowhere how could I save these logs in the text file or in database.
If ASP.NET Core already has default logging functionality, I am wondering why should I use other tools like elmah, log4net. So again when I searched for article that implements default logging to save the logs in db or in text file, I couldn't find any. Is there any way how we can save the logs in file using ASP.NET core's built in support for logging?
I am currently using Serilog which works perfectly and also downloaded seq for displaying the logs gracefully in browser. However, I am still wondering how could I achieve the same using built in asp.net core logging functionality.
By default the ASP.NET Core logging if based on the standard .NET Core abstractions and the implementations for said abstractions. The link that you providing is exactly what you want to follow for consuming the logging services. These will write to the standard output (output window) for example when debugging.
The part that you're looking for specifically is the web.config
. Consider the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"
resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%"
forwardWindowsAuthToken="false" stdoutLogEnabled="true"
stdoutLogFile="C:\temp\logs\log.log" />
</system.webServer>
</configuration>
You are looking for the stdoutLogEnabled
and stdoutLogFile
.
stdoutLogEnabled If true, stdout and stderr for the process specified in
processPath
will be redirected to the file specified instdoutLogFile
.
And
stdoutLogFile Specifies the relative or absolute file path for which stdout and stderr from the process specified in
processPath
will be logged. Relative paths are relative to the root of the site. Any path starting with ‘.’ will be relative to the site root and all other paths will be treated as absolute paths.
Also see Publishing to IIS for details on the ASP.NET Core module.