Reputation: 1400
I have existing MVC5 code where I need to integrate logging. Can anyone let me know the best way to integrate without making lot modifications in existing code. I have one approach where I am thinking of using MVC action filters for logging and DI to inject logger. Any thoughts on other combinations?
Upvotes: 1
Views: 411
Reputation: 36839
NLog Steps
1. Install NLog.Config
Install NLog with example config (nlog.config)
Install-Package NLog.Config
2. Edit config
Enable the rules in the nlog.config
3. Log the exceptions
protected void Application_Error(object sender, EventArgs e)
{
var logger = LogManager.GetLogger("application");
Exception exception = Server.GetLastError();
Response.Clear();
logger.Error(exception, "error in application"); //logs full exception
}
Upvotes: 0
Reputation: 440
You can choose Log4Net library to integrate Logging and use the Global.asax Application_Error event to log all application errors at single place. So it will not require lot modification in your existing code.
Step by step guide
Step-1 (Get Log4Net)
At first you need to download Log4Net library which you can download from here:
https://logging.apache.org/log4net/download_log4net.cgi
or alternatively you can choose nuget to download Log4Net Package in your existing application. To do this run below command in 'Package Manager Console' which you can find in Tools -> Nuget Package manager in Visual Studio.
Step-2 (Add Log4Net Reference)
Once you have downloaded Log4Net by any mean shown above, make sure you have Log4Net dll in your project references. If it's not added add reference to log4net dll.
Step-3 (Configure Log4Net)
Open AssemblyInfo.cs in your Project -> Properties folder and add below line
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
This will cause Log4Net to look for a configuration file and The config file will be watched for changes.
Step-4 (Configure Logging Approach)
Now specify Log4Net configuration in web.config Add configSections entry for Log4Net under
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
</configuration>
Also, add configuration for Log file, here I am using RollingFileAppender
<log4net>
<appender name="RollingErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\\Error\\" />
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<datePattern value="'Error_'yyyyMMdd'.log'" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="5MB" />
<staticLogFileName value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd-MMM-yyyy HH:mm:ss} [%thread] %-5level %file %line %M %logger [%property{NDC}] - %message%newline%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingErrorLogFileAppender" />
</root>
</log4net>
Step-5 (Catch Application Errors and Log them)
Your log4net configuration is done. Now it's turn to catch exception and write them into log file. If you want to catch all application exception at single place, you may use Global.asax Application_Error event for this. Create Log4Net variable at the top of Global.asax
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Then, create Application_Error event in Global.asax like shown below
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
log.Error(exception.Message);
}
This will log application errors in 'Error' folder in your application Root folder.
References
Below you can see documentation to configure Log4Net
https://logging.apache.org/log4net/release/manual/configuration.html
Hope this will help !!
Upvotes: 2