簡體   English   中英

如何在其他項目中添加NLog類庫項目

[英]How add NLog Class Library project in other projects


我在C#中創建了一個控制台項目,在該項目中我使用NLog進行日志記錄。
當我運行項目時,它成功登錄到多個目標,如Console,File,EventLog和Sentinal。
但是,當我將此項目作為classLibrary並嘗試將引用添加到另一個項目時,它不會記錄到任何目標。 當然,項目中沒有錯誤。
以下是代碼:

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");
    //}
}
}

以下是Nlog.config文件:

<?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>

我將這個項目dll添加到另一個項目,它沒有錯誤。

我正在創建logWrapper類的對象並調用該方法來記錄信息,但它根本不起作用。 請讓我知道我在這里犯的錯誤。

為什么它沒有運行:你的新項目是否有NLog.config文件?

如果您需要從所有項目的單個nlog.config加載,您:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM