簡體   English   中英

NLog在C#中獲取消息

[英]NLog get messages in C#

我最近在ASP.Net項目中為客戶取得了成功,使用了Nlog。 我需要連接到具有自己的事件日志管理的現有應用程序,並向他們提供我的日志信息的副本。

有沒有辦法使用任何日志功能來獲取我提供給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

如何獲取字符串“我的信息”,“我的調試”,“我的錯誤”和“我的警告”作為數組或List <>?

您可以使用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屬性呈現

適用於MemoryTarget的API文檔

如果您需要將它們寫入其他組件,則建議創建一個自定義目標

暫無
暫無

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

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