Log4Net:将 C# AdoNetAppender 与自定义 Xml 布局结合使用

发布于 2024-11-30 21:32:57 字数 1579 浏览 1 评论 0原文

我有点卡住了。我一直在编写一个日志记录模块,它要求附加程序能够在数据库上执行存储过程来记录我们的信息。我编写了一个自定义 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

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

任何帮助将不胜感激。

I'm a bit stuck. I've been writing a logging module which requires the appender being able to execute a stored procedure on the database to log our information. I have written a custom xml layout which is as follows:

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();
    }

So basically, i'd like the AdoNetAppender to run the logging event through this layout and then execute the storproc with the resulting Xml as the parameter. i.e.

EXEC Logging.InsertEntry @XML

Unfortunately there doesn't seem to be any examples around.

Any help would be extremely appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2024-12-07 21:32:57

像这样的东西应该可以工作(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 结构中的所有元素提供一个参数...

Something like this should work (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>

I do wonder however if you would not rather create a stored procedure that has a parameter for all the elements in your xml structure...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文