简体   繁体   中英

Log4Net: Using the C# AdoNetAppender with a custom Xml layout

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. ie

EXEC Logging.InsertEntry @XML

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

Any help would be extremely appreciated.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM