Skip to content

rabbycse/ErrorLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ErrorLogger

  • Install the nuget package or reference the project into your asp.net core application.

ngt

  • Add the AddLog4Net() call into your Configure method of the Startup.cs class.
using Microsoft.Extensions.Logging;

public class Startup
{
    //...

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        //...

        loggerFactory.AddLog4Net(); // << Add this line
        app.UseMvc();

        //...
    }
}
  • Add a log4net.config file with the content:
<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <appender name="RollingFile" type="log4net.Appender.FileAppender">
    <file value="C:\Temp\app.log" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
    </layout>
  </appender>
   <root>
    <level value="ALL" />
    <appender-ref ref="RollingFile" />
  </root>
</log4net>
  • Then log4net.config->Propertise->Copy to Output Directory->Change to Copy if newer

  • Add a HomeController.cs file with the content:

public class HomeController
{
    //...

    public HomeController(ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger("RollingFile");

            logger.LogCritical("Hello");

         #if DEBUG
                   logger.LogDebug("This is a debug log");
         #endif
         
        }
        
        //...
}

Ouput

  • Go to (c:\Temp\app.log):

log

About

ASP.NET Core logging provider using log4net

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published