Azure上的Log4Net

时间:2015-04-03 22:09:42

标签: azure logging storage log4net

我已经尝试了我可以在网上找到的所有内容,以使log4net与Azure协同工作,但由于大多数文档都已过时,因此还没有运气。如果有人使用最新的Azure SDK和Log4Net版本,并且可以将我推向正确的方向,那将非常感激。

似乎有很多不同的方法可以做到这一点,但我还没有把它附加到表格,blob或数据库,就像许多例子所示。

1 个答案:

答案 0 :(得分:2)

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

在数据库中创建日志表:

/****** 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>