2017-07-05 46 views
0

我最近在一個ASP.Net項目中爲Nlog使用了一個成功的客戶端。我需要連接到具有自己的事件日誌管理的現有應用程序,併爲他們提供我的日誌信息的副本。NLog在C#中獲取消息

有什麼辦法讓我使用任何日誌功能提供給Nlog的字符串?

例子:

logger.Info("My info"); 
logger.Debug("My debug"); 
logger.Error("My error"); 
logger.Warn("My warn"); 
List<string> messages = logger.getStrings() <-- this is what I need 

我怎樣才能得到字符串「我的信息」,「我的調試」,「我的錯誤」和「我的提醒」數組或列表<>?

+0

是你的日誌?這將是從另一個應用程序獲取日誌數據的最簡單方法。你也可以使用'MemoryAppender'並獲得它的內部緩衝區。 – Amy

+0

@amy'MemoryAppender'?聽起來像log4net? – Julian

+0

@Julian哎呀,我正在引導我的內部NLog。 – Amy

回答

2

您可以使用MemoryTarget吧:

nlog.config:

<nlog internalLogFile="c:\tempp\nlog-internal.log" internalLogLevel="Info"> 
    <targets> 
     <target xsi:type="Memory" name="target1" layout="${message}" /> 
    </targets> 
    <rules> 
     <logger name="*" writeTo="target1" /> 
    </rules> 
</nlog> 

C#:

var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1"); 
IList<string> messages = target.Logs; 

的消息strings,從nlog.config的layout屬性呈現

API docs for MemoryTarget

如果你需要將它們寫入到另一個組件,我會建議創建寫入數據庫custom target

+0

謝謝,這是我需要的。有什麼方法可以讓消息與關卡一起獲得? –

+0

當然,更改佈局屬性並添加'$ {level}' – Julian