当前位置:Gxlcms > 数据库问题 > Log4NET将日志写入数据库中

Log4NET将日志写入数据库中

时间:2021-07-01 10:21:17 帮助过:20人阅读

These examples are by no means exhaustive configurations for the appenders. For a full list of the parameters that can be specified to each appender and more details on each options see the SDK documentation for the appender.

AdoNetAppender

For full details see the SDK Reference entry: log4net.Appender.AdoNetAppender.

The configuration of the AdoNetAppender depends on the provider selected for the target database. Here are some examples.

MS SQL Server

The following example shows how to configure the AdoNetAppender to log messages to a SQL Server database. The events are written in batches of 100 (BufferSize). The ConnectionType specifies the fully qualified type name for the System.Data.IDbConnection to use to connect to the database. The ConnectionString is database provider specific. The CommandText is either a prepared statement or a stored procedure, in this case it is a prepared statement. Each parameter to the prepared statement or stored procedure is specified with its name, database type and a layout that renders the value for the parameter.

The database table definition is:

  1. CREATE TABLE [dbo].[Log] (
  2. [Id] [int] IDENTITY (1, 1) NOT NULL,
  3. [Date] [datetime] NOT NULL,
  4. [Thread] [varchar] (255) NOT NULL,
  5. [Level] [varchar] (50) NOT NULL,
  6. [Logger] [varchar] (255) NOT NULL,
  7. [Message] [varchar] (4000) NOT NULL,
  8. [Exception] [varchar] (2000) NULL
  9. )

The appender configuration is:

  1. <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  2. <bufferSize value="100" />
  3. <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  4. <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
  5. <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
  6. <parameter>
  7. <parameterName value="@log_date" />
  8. <dbType value="DateTime" />
  9. <layout type="log4net.Layout.RawTimeStampLayout" />
  10. </parameter>
  11. <parameter>
  12. <parameterName value="@thread" />
  13. <dbType value="String" />
  14. <size value="255" />
  15. <layout type="log4net.Layout.PatternLayout">
  16. <conversionPattern value="%thread" />
  17. </layout>
  18. </parameter>
  19. <parameter>
  20. <parameterName value="@log_level" />
  21. <dbType value="String" />
  22. <size value="50" />
  23. <layout type="log4net.Layout.PatternLayout">
  24. <conversionPattern value="%level" />
  25. </layout>
  26. </parameter>
  27. <parameter>
  28. <parameterName value="@logger" />
  29. <dbType value="String" />
  30. <size value="255" />
  31. <layout type="log4net.Layout.PatternLayout">
  32. <conversionPattern value="%logger" />
  33. </layout>
  34. </parameter>
  35. <parameter>
  36. <parameterName value="@message" />
  37. <dbType value="String" />
  38. <size value="4000" />
  39. <layout type="log4net.Layout.PatternLayout">
  40. <conversionPattern value="%message" />
  41. </layout>
  42. </parameter>
  43. <parameter>
  44. <parameterName value="@exception" />
  45. <dbType value="String" />
  46. <size value="2000" />
  47. <layout type="log4net.Layout.ExceptionLayout" />
  48. </parameter>
  49. </appender>

MS Access

This example shows how to write events to an Access Database.

  1. <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender">
  2. <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\log\access.mdb;User Id=;Password=;" />
  3. <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
  4. <parameter>
  5. <parameterName value="@log_date" />
  6. <dbType value="String" />
  7. <size value="255" />
  8. <layout type="log4net.Layout.PatternLayout">
  9. <conversionPattern value="%date" />
  10. </layout>
  11. </parameter>
  12. <parameter>
  13. <parameterName value="@thread" />
  14. <dbType value="String" />
  15. <size value="255" />
  16. <layout type="log4net.Layout.PatternLayout">
  17. <conversionPattern value="%thread" />
  18. </layout>
  19. </parameter>
  20. <parameter>
  21. <parameterName value="@log_level" />
  22. <dbType value="String" />
  23. <size value="50" />
  24. <layout type="log4net.Layout.PatternLayout">
  25. <conversionPattern value="%level" />
  26. </layout>
  27. </parameter>
  28. <parameter>
  29. <parameterName value="@logger" />
  30. <dbType value="String" />
  31. <size value="255" />
  32. <layout type="log4net.Layout.PatternLayout">
  33. <conversionPattern value="%logger" />
  34. </layout>
  35. </parameter>
  36. <parameter>
  37. <parameterName value="@message" />
  38. <dbType value="String" />
  39. <size value="1024" />
  40. <layout type="log4net.Layout.PatternLayout">
  41. <conversionPattern value="%message" />
  42. </layout>
  43. </parameter>
  44. </appender>

Oracle9i

This example shows how to write events to an Oracle9i Database.

The database table definition is:

  1. create table log (
  2. Datetime timestamp(3),
  3. Thread varchar2(255),
  4. Log_Level varchar2(255),
  5. Logger varchar2(255),
  6. Message varchar2(4000)
  7. );

The appender configuration is:

  1. <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
  2. <connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  3. <connectionString value="data source=[mydatabase];User ID=[user];Password=[password]" />
  4. <commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
  5. <bufferSize value="128" />
  6. <parameter>
  7. <parameterName value=":log_date" />
  8. <dbType value="DateTime" />
  9. <layout type="log4net.Layout.RawTimeStampLayout" />
  10. </parameter>
  11. <parameter>
  12. <parameterName value=":thread" />
  13. <dbType value="String" />
  14. <size value="255" />
  15. <layout type="log4net.Layout.PatternLayout">
  16. <conversionPattern value="%thread" />
  17. </layout>
  18. </parameter>
  19. <parameter>
  20. <parameterName value=":log_level" />
  21. <dbType value="String" />
  22. <size value="50" />
  23. <layout type="log4net.Layout.PatternLayout">
  24. <conversionPattern value="%level" />
  25. </layout>
  26. </parameter>
  27. <parameter>
  28. <parameterName value=":logger" />
  29. <dbType value="String" />
  30. <size value="255" />
  31. <layout type="log4net.Layout.PatternLayout">
  32. <conversionPattern value="%logger" />
  33. </layout>
  34. </parameter>
  35. <parameter>
  36. <parameterName value=":message" />
  37. <dbType value="String" />
  38. <size value="4000" />
  39. <layout type="log4net.Layout.PatternLayout">
  40. <conversionPattern value="%message" />
  41. </layout>
  42. </parameter>
  43. </appender>

Oracle8i

This example shows how to write events to an Oracle8i Database.

The database table definition is:

  1. CREATE TABLE CSAX30.LOG
  2. (
  3. THREAD VARCHAR2(255),
  4. LOG_LEVEL VARCHAR2(255),
  5. LOGGER VARCHAR2(255),
  6. MESSAGE VARCHAR2(4000)
  7. )
  8. TABLESPACE CSAX30D LOGGING

The appender configuration is:

  1. <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
  2. <connectionType value ="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  3. <connectionString value="data source=<dsname>;User ID=<userid>;Password=<password>" />
  4. <commandText value="INSERT INTO Log (Log_Level,Logger,Message) VALUES (:log_level, :logger, :message)" />
  5. <bufferSize value="250" />
  6. <parameter>
  7. <parameterName value=":log_level" />
  8. <layout type="log4net.Layout.PatternLayout">
  9. <conversionPattern value="%level" />
  10. </layout>
  11. </parameter>
  12. <parameter>
  13. <parameterName value=":logger" />
  14. <layout type="log4net.Layout.PatternLayout">
  15. <conversionPattern value="%logger" />
  16. </layout>
  17. </parameter>
  18. <parameter>
  19. <parameterName value=":message" />
  20. <dbType value="String" />
  21. <layout type="log4net.Layout.PatternLayout">
  22. <conversionPattern value="%message" />
  23. </layout>
  24. </parameter>
  25. </appender>

IBM DB2

This example shows how to write events to an IBM DB2 8.2 Database. The following syntax should also work with older DB2 database servers.

The database table definition is:

  1. CREATE TABLE "myschema.LOG" (
  2. "ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (
  3. START WITH +1
  4. INCREMENT BY +1
  5. MINVALUE +1
  6. MAXVALUE +2147483647
  7. NO CYCLE
  8. NO CACHE
  9. NO ORDER
  10. ),
  11. "DATE" TIMESTAMP NOT NULL,
  12. "THREAD" VARCHAR(255) NOT NULL,
  13. "LEVEL" VARCHAR(500) NOT NULL,
  14. "LOGGER" VARCHAR(255) NOT NULL,
  15. "MESSAGE" VARCHAR(4000) NOT NULL,
  16. "EXCEPTION" VARCHAR(2000)
  17. )
  18. IN "LRGTABLES";

The appender configuration is:

  1. <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  2. <bufferSize value="100" />
  3. <connectionType value="IBM.Data.DB2.DB2Connection,IBM.Data.DB2, Version=8.1.2.1" />
  4. <connectionString value="server=192.168.0.0;database=dbuser;user Id=username;password=password;persist security info=true" />
  5. <commandText value="INSERT INTO myschema.Log (Date,Thread,Level,Logger,Message,Exception) VALUES (@log_date,@thread,@log_level,@logger,@message,@exception)" />
  6. <parameter>
  7. <parameterName value="@log_date" />
  8. <dbType value="DateTime" />
  9. <layout type="log4net.Layout.RawTimeStampLayout" />
  10. </parameter>
  11. <parameter>
  12. <parameterName value="@thread" />
  13. <dbType value="String" />
  14. <size value="255" />
  15. <layout type="log4net.Layout.PatternLayout">
  16. <conversionPattern value="%thread" />
  17. </layout>
  18. </parameter>
  19. <parameter>
  20. <parameterName value="@log_level" />
  21. <dbType value="String" />
  22. <size value="500" />
  23. <layout type="log4net.Layout.PatternLayout">
  24. <conversionPattern value="%level" />
  25. </layout>
  26. </parameter>
  27. <parameter>
  28. <parameterName value="@logger" />
  29. <dbType value="String" />
  30. <size value="255" />
  31. <layout type="log4net.Layout.PatternLayout">
  32. <conversionPattern value="%logger" />
  33. </layout>
  34. </parameter>
  35. <parameter>
  36. <parameterName value="@message" />
  37. <dbType value="String" />
  38. <size value="4000" />
  39. <layout type="log4net.Layout.PatternLayout">
  40. <conversionPattern value="%message" />
  41. </layout>
  42. </parameter>
  43. <parameter>
  44. <parameterName value="@exception" />
  45. <dbType value="String" />
  46. <size value="2000" />
  47. <layout type="log4net.Layout.ExceptionLayout" />
  48. </parameter>
  49. </appender>

SQLite

This example shows how to write events to a SQLite Database. This was tested against v0.21 of the SQLite .NET provider.

SQLite doesn‘t have strongly-typed columns or field lengths but its recommended you still include this information for readability. The database table definition is:

  1. CREATE TABLE Log (
  2. LogId INTEGER PRIMARY KEY,
  3. Date DATETIME NOT NULL,
  4. Level VARCHAR(50) NOT NULL,
  5. Logger VARCHAR(255) NOT NULL,
  6. Message TEXT DEFAULT NULL
  7. );

The appender configuration is:

  1. <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  2. <bufferSize value="100" />
  3. <connectionType value="Finisar.SQLite.SQLiteConnection, SQLite.NET, Version=0.21.1869.3794, Culture=neutral, PublicKeyToken=c273bd375e695f9c" />
  4. <connectionString value="Data Source=c:\\inetpub\\wwwroot\\logs\\log4net.db;Version=3;" />
  5. <commandText value="INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" />
  6. <parameter>
  7. <parameterName value="@Date" />
  8. <dbType value="DateTime" />
  9. <layout type="log4net.Layout.RawTimeStampLayout" />
  10. </parameter>
  11. <parameter>
  12. <parameterName value="@Level" />
  13. <dbType value="String" />
  14. <layout type="log4net.Layout.PatternLayout">
  15. <conversionPattern value="%level" />
  16. </layout>
  17. </parameter>
  18. <parameter>
  19. <parameterName value="@Logger" />
  20. <dbType value="String" />
  21. <layout type="log4net.Layout.PatternLayout">
  22. <conversionPattern value="%logger" />
  23. </layout>
  24. </parameter>
  25. <parameter>
  26. <parameterName value="@Message" />
  27. <dbType value="String" />
  28. <layout type="log4net.Layout.PatternLayout">
  29. <conversionPattern value="%message" />
  30. </layout>
  31. </parameter>
  32. </appender>

AspNetTraceAppender

For full details see the SDK Reference entry: log4net.Appender.AspNetTraceAppender.

The following example shows how to configure the AspNetTraceAppender to log messages to the ASP.NET TraceContext. The messages are written to the System.Web.TraceContext.Write method if they are below level WARN. If they are WARN or above they are written to theSystem.Web.TraceContext.Warn method.

  1. <appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" >
  2. <layout type="log4net.Layout.PatternLayout">
  3. <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  4. </layout>
  5. </appender>

BufferingForwardingAppender

For full details see the SDK Reference entry: log4net.Appender.BufferingForwardingAppender.

The following example shows how to configure the BufferingForwardingAppender to buffer 100 messages before delivering them to the ConsoleAppender.

  1. <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
  2. <bufferSize value="100"/>
  3. <appender-ref ref="ConsoleAppender" />
  4. </appender>

This example shows how to deliver only significant events. A LevelEvaluator is specified with a threshold of WARN. This means that the events will only be delivered when a message with level of WARN or higher level is logged. Up to 512 (BufferSize) previous messages of any level will also be delivered to provide context information. Messages not sent will be discarded.

  1. <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" >
  2. <bufferSize value="512"

人气教程排行