Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Core 2.0 Webapi set log4net

I am new to asp.net core 2.0. I creating a webapi and want to configure log4net as my logging provider.

I couldn't find some working example in net. Can anyone help me provide the right link or some sample code about how to properly setup log4net in asp.net core 2.0

Things i have done so far is as below and i am not sure how to proceed further.

installed log4net.Extensions.AspNetCore;

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    loggerFactory.AddLog4Net();

    // Enable CORS 
    app.UseCors(builder => builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials());

    app.UseMvc();
}
like image 327
Mukil Deepthi Avatar asked Dec 18 '17 10:12

Mukil Deepthi


People also ask

Is log4net compatible with .NET core?

NET Core. To use log4net from . NET core, you need to install at least version 2.0.

Does log4net support .NET Core 6?

yes. Log4Net targets . NET Standard, which gives it a level of compatibility across the entirety of . NET core, including .


1 Answers

  1. Install nuget for Microsoft.Extensions.Logging.Log4Net.AspNetCore (https://www.nuget.org/packages/Microsoft.Extensions.Logging.Log4Net.AspNetCore/)
  2. In Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) call loggerFactory.AddLog4Net();
  3. Create log4net.config

More info -> https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore

like image 104
aksu Avatar answered Sep 27 '22 17:09

aksu