Log4Net:使用C#AdoNetAppender和自定义Xml布局

时间:2011-08-19 21:58:08

标签: c# xml log4net adonetappender

我有点卡住了。我一直在编写一个日志记录模块,它要求appender能够在数据库上执行存储过程来记录我们的信息。我编写了一个自定义的xml布局,如下所示:

protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
    {
        writer.WriteStartElement("LogEntry");

        writer.WriteStartElement("DateTime");
        writer.WriteString(loggingEvent.TimeStamp.ToLongTimeString());
        writer.WriteEndElement();

        writer.WriteStartElement("Message");
        writer.WriteString(loggingEvent.RenderedMessage);
        writer.WriteEndElement();

        writer.WriteStartElement("User");
        writer.WriteString(loggingEvent.UserName);
        writer.WriteEndElement();

        writer.WriteStartElement("Process");
        writer.WriteString(loggingEvent.ThreadName);
        writer.WriteEndElement();

        writer.WriteStartElement("Severity");
        writer.WriteString(loggingEvent.Level.ToString());
        writer.WriteEndElement();

        writer.WriteStartElement("Title");
        writer.WriteString(loggingEvent.LoggerName);
        writer.WriteEndElement();

        writer.WriteStartElement("Class");
        writer.WriteString(loggingEvent.LocationInformation.ClassName);
        writer.WriteEndElement();

        writer.WriteStartElement("Filename");
        writer.WriteString(loggingEvent.LocationInformation.FileName);
        writer.WriteEndElement();

        writer.WriteEndElement();
    }

基本上,我希望AdoNetAppender通过这种布局运行日志事件,然后以生成的Xml作为参数执行storproc。即

EXEC Logging.InsertEntry @XML

不幸的是,似乎没有任何例子。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这样的东西应该可以工作(SQL Server):

<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
    <bufferSize value="1" />
    <threshold value="ALL"/>
    <param name="ConnectionType" value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <param name="ConnectionString" value="YourConnectionString" />
    <param name="UseTransactions" value="False" />
    <commandText value="Logging.InsertEntry" />
    <commandType value="StoredProcedure" />
    <parameter>
        <parameterName value="XML"/>
        <dbType value="String"/>         
        <layout type="YourLayoutType">           
    </parameter>
</appender>

我确实不知道你是不是宁愿创建一个存储过程,它有一个xml结构中所有元素的参数......