3
我已經在C#中創建了一個控制檯項目,並且在該項目中我使用NLog進行日誌記錄。
當我運行該項目時,它成功登錄到多個目標,如控制檯,文件,事件日誌以及Sentinal。
但是,當我將此項目作爲classLibrary並嘗試將引用添加到另一個項目時,它不會記錄到任何目標。當然,這個項目沒有錯誤。
以下是代碼:
如何在其他項目中添加NLog類庫項目
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項目作爲一個單獨的項目,並將其納入'n'個項目中。 –
每個項目都需要定義配置。 DLL在許多項目中共享,但每個項目可以有不同的登錄方式。所以,每個項目應該有自己的nlog.config文件。 –
如果我在每個引用NLog的項目中都包含配置文件,那麼我打算將所有項目的單個Nlog.config文件打散。我需要在NLog項目中維護一個NLog.config文件,而不是在其他任何項目中。 –