2012-08-24 78 views
2

我想使用NLog登錄到RavenDb數據庫。顯然沒有現有的NLog目標(根據this和搜索),所以我試圖寫我自己的。我一直與RavenDb客戶端自己的NLog配置發生衝突,我無法解決如何解決這個問題。這是我的目標:RavenDb NLog配置衝突

namespace NLog.RavenDb 
{ 
    class LogEntry 
    { 
     public string Id { get; set; } 
     public Exception Exception { get; set; } 
     public string LogLevel { get; set; } 
     public StackTrace StackTrace { get; set; } 
     public DateTimeOffset TimeStamp { get; set; } 
     public string Message { get; set; } 
    } 

    public class RavenDbTarget : NLog.Targets.TargetWithLayout 
    { 
     public static IDocumentStore Store { get; set; } 

     protected override void Write(LogEventInfo logEvent) 
     { 
      if (Store == null) 
      { 
       const string noStoreWarning = "No Document Store set for the RavenDb Log target"; 
       Debug.WriteLine(noStoreWarning); 
       return; 
      } 

      LogEntry entry = new LogEntry 
      { 
       Exception = logEvent.Exception, 
       LogLevel = logEvent.Level.Name, 
       Message = logEvent.FormattedMessage, 
       StackTrace = logEvent.StackTrace, 
       TimeStamp = new DateTimeOffset(logEvent.TimeStamp) 
      }; 

      using (var session = Store.OpenSession()) 
      { 
       session.Store(entry); 
       session.SaveChanges(); 
      } 
     } 
    } 
} 

而這裏的我怎麼才能得到它與SimpleConfigurator工作:

RavenDbTarget.Store = new DocumentStore { Url = "http://localhost:8080/" }; 
RavenDbTarget.Store.Initialize(); 

NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(new RavenDbTarget()); 
Logger logger = NLog.LogManager.GetLogger("Any"); 
logger.Info("Hi"); 

然而,NLog.config所使用的RavenDb太多,所以當我把引用我的目標在那裏,它在Store.Initialize()中引發異常。這是我在NLog.config試過(NLog.RavenDb是包含RavenDb我的目標庫項目的名稱):

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

    <extensions> 
     <add assembly="Nlog.RavenDb"/> 
    </extensions> 

    <targets> 
     <target xsi:type="NLog.RavenDb.RavenDbTarget" name="Raven"/> 
     <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" 
       layout="${longdate} ${uppercase:${level}} ${message}" /> 
    </targets> 

    <rules> 
     <logger minlevel="Trace" name="Any" writeTo="Raven" /> 
    </rules> 
</nlog> 

我怎樣才能讓我的NLog.config文件發揮好與RavenDb的利用呢?

回答

4

找到它。我錯過了我的目標課程上的目標屬性,如here所示。

[NLog.Targets.Target("RavenDb")] 
public class RavenDbTarget : NLog.Targets.TargetWithLayout 

傳遞給目標屬性的名稱參數是你引用作爲您的配置文件「類型」的一個:

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

    <extensions> 
     <add assembly="Nlog.RavenDb"/> 
    </extensions> 

    <targets> 
     <target xsi:type="RavenDb" name="Raven"/> 
    </targets> 

    <rules> 
     <logger minlevel="Trace" name="Any" writeTo="Raven" /> 
    </rules> 
</nlog> 

現在NLOG自動拾取NLog.config,和我不不需要任何額外的配置代碼。這裏是我的測試控制檯應用程序的主要方法現在的內容:

 RavenDbTarget.Store = new DocumentStore { Url = "http://localhost:8080/" }; 
     RavenDbTarget.Store.Initialize(); 

     Logger logger = LogManager.GetLogger("Any"); 
     logger.Info("Hi"); 
+0

不錯,我一直在尋找這個。 – user981375