2014-05-23 26 views
2

可以說,我已經在我的nlog.config以下(從http://nlog-project.org/documentation/v2.0.1/html/T_NLog_Targets_MemoryTarget.htm拍攝):存取存儲器目標在NLOG

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <targets> 
     <target name="memory" xsi:type="Memory" layout="${message}" /> 
    </targets> 

    <rules> 
     <logger name="*" minlevel="Info" writeTo="memory" /> 
    </rules> 
</nlog> 

如何編程訪問此目標?我正試圖在文本框中顯示日誌。

回答

-1

您可以創建自己的目標和過程的日誌條目需要:https://github.com/nlog/NLog/wiki/How-to-write-a-Target

+0

爲什麼他需要這樣做,當NLog有一個「MemoryTarget」已經寫出來只是爲了這個目的? – Amy

+1

使用MemoryTarget時,您必須手動/定期檢查新日誌條目。此外,也許你必須過濾已經呈現的腿部條目,或者改變整個文本框內容,這不是很有效。使用自定義目標,您可以根據需要通知可以處理添加日誌條目的偵聽器 - 在這種情況下,可以附加到TextBox!畢竟,這只是一個選擇,但我不同意downvote ... – user3636971

+0

你可能是downvoted,因爲你沒有回答OP非常具體的問題。 「我如何以編程方式訪問此目標?」答案不是如何創建不同類型的目標。 – Amy

2

您可以使用LoggingConfiguration.FindTargetByName傳遞目標的名稱,然後將其轉換爲MemoryTarget,並使用日誌屬性來獲取日誌收集

3

這裏完全同樣的問題,這個工作對我來說:

var target =(MemoryTarget)LogManager.Configuration.FindTargetByName("memory"); 
var log = string.Join("\r\n", target.Logs); 
txtLog.Text = log; 
1

入住這裏NLOG手動http://nlog-project.org/documentation/v2.0.1/html/T_NLog_Targets_MemoryTarget.htm

using System; 

using NLog; 
using NLog.Targets; 

class Example 
{ 
    static void Main(string[] args) 
    { 
     MemoryTarget target = new MemoryTarget(); 
     target.Layout = "${message}"; 
     // target.Layout = "${longdate} ${level:uppercase=true} ${logger} ${message}"; 
     NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 

     Logger logger = LogManager.GetLogger("Example"); 
     logger.Debug("log message"); 

     foreach (string s in target.Logs) 
     { 
      Console.Write("logged: {0}", s); 
     } 
    } 
}