rabbycse / ErrorLogger

ASP.NET Core logging provider using log4net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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


Languages

Language:C# 69.0%Language:HTML 25.1%Language:CSS 3.3%Language:Dockerfile 1.9%Language:JavaScript 0.7%