How to log MethodName when wrapping Log4net?

后端 未结 10 1720
说谎
说谎 2020-11-29 23:41

I have wrapped Log4net in a static wrapper and want to log

loggingEvent.LocationInformation.MethodName
loggingEvent.LocationInformation.ClassName
         


        
10条回答
  •  旧时难觅i
    2020-11-30 00:45

    Click here to learn how to implement log4net in .NET Core 2.2

    The following steps are taken from the above link, and break down how to add log4net to a .NET Core 2.2 project.

    First, run the following command in the Package-Manager console:

    Install-Package Log4Net_Logging -Version 1.0.0
    

    Then add a log4net.config with the following information (please edit it to match your set up):

    
    
      
        

    Then, add the following code into a controller (this is an example, please edit it before adding it to your controller):

    public ValuesController()
    {
        LogFourNet.SetUp(Assembly.GetEntryAssembly(), "log4net.config");
    }
    // GET api/values
    [HttpGet]
    public ActionResult> Get()
    {
        LogFourNet.Info(this, "This is Info logging");
        LogFourNet.Debug(this, "This is Debug logging");
        LogFourNet.Error(this, "This is Error logging");    
        return new string[] { "value1", "value2" };
    }
    

    Then call the relevant controller action (using the above example, call /Values/Get with an HTTP GET), and you will receive the output matching the following:

    2019-06-05 19:58:45,103 [9] INFO-[Log4NetLogging_Project.Controllers.ValuesController.Get:23] - This is Info logging

提交回复
热议问题