0

我有点卡住了。我一直在编写一个日志模块,它要求 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。IE

EXEC Logging.InsertEntry @XML

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

任何帮助将不胜感激。

4

1 回答 1

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 结构中所有元素的参数...

于 2011-08-21T10:45:05.933 回答