Log4Net is extremely flexible in that it will support logging to a multitude of targets. Since this project requires SQL Server anyway in order to store the data from Twitter, you may want to store logging data there as well. Here is the procedure to enable logging to SQL Server:

Create a table to hold the log data.

This is merely a sample, you could add or delete columns as appropriate for your environment. For a complete list of supported columns see the Log4Net documentation. Also be aware that the columns specified in your config file need to match the columns in your table.

CREATE TABLE [dbo].[Log](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar](255) NOT NULL,
    [Level] [varchar](50) NOT NULL,
    [Logger] [varchar](255) NOT NULL,
    [Message] [varchar](4000) NOT NULL,
    [Exception] [varchar](2000) NULL
) ON [PRIMARY]

 

Modify the Log4Net.config file

 

Here is a sample file that will send logging messages to both the console and SQL Server.  Be sure to modify the connection string as appropriate for your environment.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <log4net>
    <root>
      <level value="All"/>
      <appender-ref ref="ConsoleAppender" />
      <appender-ref ref="AdoNetAppender"/>
    </root>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE}
          [%thread] %level %logger - %message%newline"/>
      </layout>
    </appender>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100"/>
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
     <connectionString value="data source=xxxxx;initial catalog=Twitter;integrated security=False;User=xxxxx;Password=xxxxx"/>
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)"/>
      <parameter>
        <parameterName value="@log_date"/>
        <dbType value="DateTime"/>
        <layout type="log4net.Layout.RawUtcTimeStampLayout"/>
      </parameter>
      <parameter>
        <parameterName value="@thread"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level"/>
        <dbType value="String"/>
        <size value="50"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger"/>
        <dbType value="String"/>
        <size value="255"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message"/>
        <dbType value="String"/>
        <size value="4000"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message"/>
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception"/>
        <dbType value="String"/>
        <size value="2000"/>
        <layout type="log4net.Layout.ExceptionLayout"/>
      </parameter>
    </appender>
  </log4net>

</configuration>

Last edited Mar 11, 2014 at 9:43 AM by JonnyBoats, version 1