RavenDb NLog配置冲突

时间:2012-08-24 12:55:46

标签: c# ravendb nlog

我想使用NLog登录RavenDb数据库。显然没有现成的NLog Target(根据this和搜索),所以我试图编写自己的。我一直与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");

但是,Nven.config也被RavenDb使用,所以当我在那里引用我的Target时,它会在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的使用中发挥得淋漓尽致?

1 个答案:

答案 0 :(得分:4)

找到它。我错过了目标类的Target属性,如here所示。

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

传递给Target属性的name参数是您在配置文件中引用为“type”的参数:

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