Azure 上的 Log4Net

Log4Net on Azure

我已经尝试了所有可能在网上找到的方法来让 log4net 与 Azure 一起工作,但还没有成功,因为大多数文档都已经过时了。如果有人可以使用最新的 Azure SDK 和 Log4Net 版本,并且可以将我推向正确的方向,将不胜感激。

似乎有很多不同的方法可以做到这一点,但我还没有像许多示例所建议的那样将其附加到 table、blob 或数据库。

要在 Azure 网站或 Web 角色中将 SQL 或 Azure SQL 与 log4net 一起使用:

在您的数据库中创建日志 table:

/****** Object:  Table [dbo].[Log]    Script Date: 4/3/2015 6:32:43 PM 
******/
SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Log](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NULL,
    [Level] [nvarchar](50) NULL,
    [Message] [nvarchar](4000) NULL,
    [Exception] [nvarchar](4000) NULL,
    [RemoteHost] [nvarchar](50) NULL,
    [Cookie] [nvarchar](4000) NULL,
    [Url] [nvarchar](200) NULL,
    [QueryString] [nvarchar](1000) NULL,
    [Referrer] [nvarchar](200) NULL,
    [RequestMethod] [nvarchar](10) NULL,
    [ScriptPath] [nvarchar](150) NULL,
    [UserAgent] [nvarchar](1000) NULL,
    [Domain] [nvarchar](50) NULL,
    [CustomBlob] [varchar](max) NULL,
 CONSTRAINT [PrimaryKey_11579ccd-53dd-4bcc-a813-cb4cfb7b6b88] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

SET ANSI_PADDING OFF
GO

修改您的 web.config 文件:

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,  Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="...[your connectionString]..." />
      <commandText value="INSERT INTO Log ([Date],[Level],[Message],[Exception],[RemoteHost],[Cookie],[Url],[QueryString],[Referrer],[RequestMethod],[ScriptPath],[UserAgent],[Domain],[CustomBlob]) VALUES                     (@log_date, @log_level, @message, @exception,@remote_host,@cookie,@url,@query_string,@referrer,@request_method,@script_path,@user_agent,@domain,@custom_blob)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </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="@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="4000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
      <parameter>
        <parameterName value="@remote_host" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{RemoteHost}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@cookie" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{Cookie}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@url" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{Url}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@query_string" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{QueryString}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@referrer" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{Referrer}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@request_method" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{RequestMethod}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@script_path" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{ScriptPath}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@user_agent" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{UserAgent}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@domain" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{Domain}" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@custom_blob" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%property{CustomBlob}" />
        </layout>
      </parameter>
    </appender>

    <root>
      <level value="All" />
      <appender-ref ref="AdoNetAppender" />
    </root>
  </log4net>