简体   繁体   中英

Nlog Write logs in files by conditions

I have the following targets in my Nlog config file

<targets>
<target name ="loggerCreatePortal" xsi:type="File" 
        fileName="c:/CRM Trace Logs/Portal2.0/${shortdate}_createPortalRegForm.log"
        layout="${logger} | ${longdate} | ${level:uppercase=true} | ${message}"
        createDirs="true"/>

<target name ="loggerCreateRegData" xsi:type="File"
        fileName="c:/CRM Trace Logs/Portal2.0/${shortdate}_createPortalRegFormData.log"
        layout="${logger} | ${longdate} | ${level:uppercase=true} | ${message}"
        createDirs="true"/>

</targets>

<rules>

<logger name="loggerCreatePortal" minlevel="Debug" writeTo="createPortalRegForm"/>
<logger name="loggerCreateRegData" minlevel="Debug" writeTo="createPortalRegFormData"/>

</rules>

In my Main class:

public class PortalController : ApiController
{
    private static readonly Logger loggerCreatePortal = LogManager.GetLogger("loggerCreatePortal");
    private static readonly Logger loggerCreateRegData = LogManager.GetLogger("loggerCreateRegData");

    [HttpPost]
    [Route("createPortalRegForm")]
    public Response createPortalRegForm([FromBody] ExpressPoll expressPoll)
    {
        loggerCreatePortal.Info("Test 1");
        IOrganizationService _orgService = CrmConnect.GetConnection();
        ...
    }

    [HttpPost]
    [Route("createPortalRegFormData")]
    public Response createPortalRegFormData([FromBody] FilledExpressPoll filledExpressPoll)
    {
        loggerCreateRegData.Info("Test 2");
        IOrganizationService _orgService = CrmConnect.GetConnection();
        ...
    }

I want to write the logs from CrmConnect class to the log file that is used for specified logger. If an app calls route CreatePortalRegForm the logs from CrmConnect class sholud be write only in file for target loggerCreatePortal.

My CrmConnect class

public static class CrmConnect
{
    private static readonly Logger loggerConn = LogManager.GetLogger("loggerConn");

    public static IOrganizationService GetConnection()
    {
       ....;
       if (conn.IsReady)
       loggerConn.Info("Conn successfull");
       ....
    }

What target i need to add or rules to my config file?

You will have to add a rule for your target, specyfing the class from which you want to capture logs, and which log level:

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" />
    </targets>

    <rules>
        <logger name="My.NameSpace.ClassName" minlevel="Info" writeTo="logconsole" />
    </rules>

in this example the target logfile will only get the logs from ClassName from My.Namespace Namespace

Your rules are pointing to non-existing target-names:

<rules>
   <logger name="loggerCreatePortal" minlevel="Debug" writeTo="createPortalRegForm"/>
   <logger name="loggerCreateRegData" minlevel="Debug" writeTo="createPortalRegFormData"/>
</rules>

This might work:

<targets>
   <target name="createPortalRegForm" xsi:type="File" 
        fileName="c:/CRM Trace Logs/Portal2.0/${shortdate}_createPortalRegForm.log"
        layout="${logger} | ${longdate} | ${level:uppercase=true} | ${message}"
        createDirs="true"/>
   <target name="createPortalRegFormData" xsi:type="File"
        fileName="c:/CRM Trace Logs/Portal2.0/${shortdate}_createPortalRegFormData.log"
        layout="${logger} | ${longdate} | ${level:uppercase=true} | ${message}"
        createDirs="true"/>
</targets>

<rules>
   <logger name="CrmConnect" minlevel="Debug" writeTo="createPortalRegForm"/>
   <logger name="loggerCreatePortal" minlevel="Debug" writeTo="createPortalRegForm"/> 
   <logger name="loggerCreateRegData" minlevel="Debug" writeTo="createPortalRegFormData"/>
</rules>
  • <logger name= should match the LogManager.GetLogger(name)
  • <logger writeTo= should match the <target name="

See also: https://github.com/nlog/nlog/wiki/Tutorial , and how <logger name -filter is working: https://github.com/nlog/nlog/wiki/Configuration-file#logger-name-filter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM