Log4net independent Configuration File Settings, log4net configuration file

Source: Internet
Author: User
Tags log4net

Log4net independent Configuration File Settings, log4net configuration file

1. Create a configuration file log4net. config in the same way as web. config or app. config;

<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>
</ConfigSections>

<Log4net debug = "true">
<Logger name = "myLogger">
<Level value = "INFO"/>
<Appender-ref = "AdoNetAppender_SqlServer"/>
</Logger>

<! -- SQL server database 1 -->
<Appender name = "AdoNetAppender_SqlServer" type = "log4net. Appender. AdoNetAppender">
<! -- BufferSize indicates the buffer size. Only five logs are written to the database. -->
<! -- Or <param name = "BufferSize" value = "10"/> -->
<BufferSize value = "0"/>
<! -- Reference -->
<! -- 2.0 this is another configuration corresponding to sql2008 if it is 2000 or 2005 -->
<ConnectionType value = "System. Data. SqlClient. SqlConnection, System. Data, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>

<! -- Connect to the database string -->
<ConnectionString value = "Data Source =.; Initial Catalog = Log; User ID = sa; Password = 123456;"/>
<! -- Insert to table Log -->
<CommandText value = "insert into Mylogger ([EVENTTYPE], [TIMESTAMP], [EVENTCATEGORY], [EVENT_ID], [COMPUTERNAME], [MAC_ADDRESS], [USERNAME], [SOURCETYPE], [SOURCE], [DESCRIPTION], [COLLECTDATE]) VALUES (@ Event_Type, @ log_date, @ EventCategory, @ Event_ID, @ ComputerName, @ Mac_Address, @ UserName, @ SourceType, @ Source, @ Description, @ CollectDate) "/>

<! -- Log type, all of which are 3 -->
<Parameter>
<ParameterName value = "@ Event_Type"/>
<DbType value = "Int32"/>
<! -- <DbType value = "String"/>
<Size value = "50"/> -->
<! -- LogComponent is the namespace where the class is located, and MyLayout is the class where the custom attribute is located. This is the part we want to write. It will be introduced below. -->
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<! -- When property is used, it indicates that this is a user-defined field attribute, which is not provided in log4net. -->
<ConversionPattern value = "% property {Event_Type}"/>
</Layout>
</Parameter>

<! -- Log record time. RawTimeStampLayout is the default time output format. -->
<Parameter>
<ParameterName value = "@ log_date"/>
<DbType value = "DateTime"/>
<Layout type = "log4net. Layout. RawTimeStampLayout"/>
<! --/Here is the log time provided by log4net -->
</Parameter>

<! -- Log category description -->
<Parameter>
<ParameterName value = "@ EventCategory"/>
<DbType value = "String"/>
<Size value = "50"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {EventCategory}"/>
</Layout>
</Parameter>

<! -- Log classification number -->
<Parameter>
<ParameterName value = "@ Event_ID"/>
<DbType value = "Int32"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {Event_ID}"/>
</Layout>
</Parameter>

<! -- Computer IP -->
<Parameter>
<ParameterName value = "@ ComputerName"/>
<DbType value = "String"/>
<Size value = "50"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {ComputerName}"/>
</Layout>
</Parameter>

<! -- Computer Mac information -->
<Parameter>
<ParameterName value = "@ Mac_Address"/>
<DbType value = "String"/>
<Size value = "50"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {Mac_Address}"/>
</Layout>
</Parameter>

<! -- Login system username -->
<Parameter>
<ParameterName value = "@ UserName"/>
<DbType value = "String"/>
<Size value = "50"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {UserName}"/>
</Layout>
</Parameter>

<! -- Event source type. The default value is Rier. -->
<Parameter>
<ParameterName value = "@ SourceType"/>
<DbType value = "String"/>
<Size value = "20"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {SourceType}"/>
</Layout>
</Parameter>

<! -- Event source -->
<Parameter>
<ParameterName value = "@ Source"/>
<DbType value = "String"/>
<Size value = "50"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {Source}"/>
</Layout>
</Parameter>

<! -- Event description -->
<Parameter>
<ParameterName value = "@ Description"/>
<DbType value = "String"/>
<Size value = "4000"/>
<Layout type = "SuperAuth. Infrastructure. MyLayout">
<ConversionPattern value = "% property {Description}"/>
</Layout>
</Parameter>

<! -- Log collection time -->
<Parameter>
<ParameterName value = "@ CollectDate"/>
<DbType value = "DateTime"/>
<Layout type = "log4net. Layout. RawTimeStampLayout"/>
</Parameter>
</Appender>

</Log4net>
<System. web>
<Compilation debug = "true" targetFramework = "4.6.2"/>
<HttpRuntime targetFramework = "4.6.2"/>
</System. web>

</Configuration>

2. Create related class files as follows:

Public class LogHelper
{
Private LogHelper ()
{
SetConfig ();
}

Private static readonly log4net. ILog loginfo = LogManager. GetLogger ("myLogger ");

Private static bool IsLoadConfig = false;
Private static void SetConfig ()
{
XmlConfigurator. Configure ();

}

/// <Summary>
/// Record logs
/// </Summary>
/// <Param name = "info"> message </param>
Public static void WriteLog (string info)
{
If (! IsLoadConfig)
{
SetConfig ();
IsLoadConfig = true;
}
If (loginfo. IsInfoEnabled)
{
Loginfo. Info (info );
}
}

/// <Summary>
/// Record logs
/// </Summary>
/// <Param name = "info"> message </param>
Public static void WriteLog (object info)
{
If (! IsLoadConfig)
{
SetConfig ();
IsLoadConfig = true;
}
If (loginfo. IsInfoEnabled)
{
Loginfo. Info (info );
}
}

/// <Summary>
/// Record exception
/// </Summary>
/// <Param name = "info"> error </param>
/// <Param name = "ex"> Exception </param>
Public static void WriteLog (string info, Exception ex)
{
If (! IsLoadConfig)
{
SetConfig ();
IsLoadConfig = true;
}
If (loginfo. IsErrorEnabled)
{
Loginfo. Error (info, ex );
}


}

3. Introduce log4net. config In the usage class as follows:

[Assembly: XmlConfigurator (ConfigFile = @ "Log4net. config", Watch = true)]

4. The most important step is where many people make mistakes. Right-click log4net. config and click Properties and set as follows.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.