Learner
Learner

Reputation: 353

Capture controller name and method name using NLog in dotnet core

I'm trying to capture controller name and method name in a dotnet core application using NLog library. Below set of codes are used in config to capture them. I'm able to get controller and action name, but not getting the expected result.

Nlog Config file:

 <targets>   
  <target xsi:type="File" archiveAboveSize="10000000" archiveNumbering="DateAndSequence" name="eventLog" fileName="D:\MiningApp\Logs\MiningApp_Log_${shortdate}.log"
    layout="${longdate} | ${uppercase:${level}} | ${callsite} | ${logger} | ${message}" />
 </targets>

Current Result:

2021-07-21 15:34:17.1299 | INFO | MiningApp.Controllers.MasterController.GetUsers | MiningApp.Controllers.MasterController | Logging started for department
2021-07-21 15:34:17.2165 | INFO | MiningApp.Controllers.MasterController+<GetUsers>d__42.MoveNext | MiningApp.Controllers.MasterController | Logging ended for department

Expected Result:

2021-07-21 15:34:17.1299 | INFO | ControllerName: MiningApp.Controllers.MasterController | MethodName : MiningApp.Controllers.MasterController.GetUsers | Logging started for department
2021-07-21 15:34:17.1376 | INFO | ControllerName: MiningApp.Controllers.MasterController | MethodName : MiningApp.Controllers.MasterController.GetUsers | Logging ended for department

Also I need to set file limit to 10MB and when the file reaches the size then it should create a new file. File names should be like Ex: MiningApp_Log_21.07.2021_01, MiningApp_Log_21.07.2021_02. Really appreciate any inputs or suggestions.

Upvotes: 1

Views: 1539

Answers (1)

Costa
Costa

Reputation: 2934

For your expected result, this should work:

<targets>   
<target xsi:type="File" archiveAboveSize="10000000" archiveNumbering="DateAndSequence" name="eventLog" fileName="D:\MiningApp\Logs\MiningApp_Log_${shortdate}.log"
  layout="${longdate} | ${uppercase:${level}} | ControllerName: ${callsite:methodName=false} | MethodName: ${callsite:includeSourcePath=false} | ${message}" />
</targets>

Configured from reading the docs at: https://nlog-project.org/config/?tab=layout-renderers

Upvotes: 3

Related Questions