简体   繁体   中英

How add NLog Class Library project in other projects


I have created a console project in C# and in that project I am using NLog for logging purpose.
When I run the project, it successfully logging into to multiple targets like Console, File, EventLog and also to Sentinal.
But when I make this project as a classLibrary and try to add the reference to another project, it's not logging to any of the targets. And of course there is no error in the project.
Following is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using NLog.Config;

namespace NLogger
{
public class logWrapper
{
    private static Logger logger = LogManager.GetCurrentClassLogger();
    public void LevelLogger(string level, string msg)
    {
        if (level == "Trace")
            Trace(msg);
        else if (level == "Info")
            Info(msg);
        else if (level == "Error")
            Error(msg);
        else if (level == "Debug")
            Debug(msg);
        else if (level == "Warn")
            Warn(msg);
    }

    public void Trace(string msg)
    {
        try
        {
            logger.Trace(msg);
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    public void Info(string msg)
    {
        logger.Info(msg);
    }

    public void Error(string msg)
    {
        logger.Error(msg);
    }
    public void Debug(string msg)
    {
        logger.Debug(msg);
    }
    public void Warn(string msg)
    {
        logger.Warn(msg);
    }

    //Uncomment following when the project type is Console App. --Debugging purpose only

    //public static void Main(string[] args)
    //{
    //    logWrapper l = new logWrapper();
    //    l.LevelLogger("Info", "INFORMATION");
    //}
}
}

Following is Nlog.config file:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<targets>
<target xsi:type="File" name="file" layout="${longdate}|${level}|${message}|${callsite:fileName=true}${newLine}"
        fileName="C:\Users\sharathk\Desktop\ToCompare\${shortdate}_Log.txt"/>
<target name="viewer" xsi:type="NLogViewer" address="udp://10.100.18.166:9999"/>
<target xsi:type="EventLog" name="event" layout="${longdate}|${level}|${message}|${callsite:fileName=true}" source="NLogger"/>
<target name="console" xsi:type="Console"
        layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
 </targets>

 <rules>
  <!-- add your logging rules here -->
 <logger name="*" minlevel="Trace" writeTo="file,event,viewer,console" />
 </rules>
</nlog>

I am adding this project dll to another project and its not working with no error.

I am creating the object to logWrapper class and calling the method to log the information, but it's not working at all. Kindly let me know the mistake I am doing here.

WHY it did not run: Does your NEW project have that NLog.config file?

IF you need to load from aa single nlog.config for all the projects, you:

NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration( "<your path>" + "\\NLog.config", true);

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