• log4netdemo


     

    Install-Package log4net -Version 2.0.12

    • 数据库建表

    CREATE TABLE [dbo].[WebLog_Msg](

        [LogID] [int] IDENTITY(1,1) NOT NULL,

        [Date] [datetime] NOT NULL,

        [Thread] [nvarchar](255) NULL,

        [Level] [nvarchar](50) NULL,

        [Logger] [nvarchar](255) NULL,

        [Message] [nvarchar](2000) NULL,

        [Exception] [nvarchar](4000) NULL,

    CONSTRAINT [PK_WebLog_Msg] PRIMARY KEY CLUSTERED

    (

        [LogID] ASC

    ))

     

    • D:vs2017projlog4netdemoPropertiesAssemblyInfo.cs 添加内容

    [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

     

     

    • D:vs2017projlog4netdemoLoggHelper.cs 内容

    using System;

     

    namespace LogHelper

    {

    public class LoggHelper

    {

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(LoggHelper));

    public static void LogInfo(Exception ex)

    {

    log.Info(ex);

    }

    public static void LogWarn(Exception ex)

    {

    log.Warn(ex);

    }

    public static void LogFatal(Exception ex)

    {

    log.Fatal(ex);

    }

    public static void LogError(Exception ex)

    {

    log.Error(ex);

    }

    }

    }

    • 配置文件

    log4net.config

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

    <configuration>

        <!--Log4Net config section-->

        <configSections>

            <!--<sectionGroup name="common">

    <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />

    </sectionGroup>-->

            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

        </configSections>

        <!--<common>

    <logging>

    <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">

    <arg key="configType" value="INLINE"/>

    </factoryAdapter>

    </logging>

    </common>-->

        <log4net>

            <root>

                <!--OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL-->

                <level value="ALL"/>

                <appender-ref ref="AdoNetAppender_SqlServer"/>

                <appender-ref ref="RollingLogFileAppender"/>

            </root>

            <!--<logger name="ErrorInfo">

    <level value="ALL" />

    <appender-ref ref="RollingLogFileAppender" />

    </logger>-->

            <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

                <param name="File" value="logerrorInfo.txt"/>

                <param name="AppendToFile" value="true"/>

                <param name="MaxSizeRollBackups" value="10"/>

                <param name="MaximumFileSize" value="2MB"/>

                <param name="RollingStyle" value="Size"/>

                <param name="StaticLogFileName" value="true"/>

                <layout type="log4net.Layout.PatternLayout">

                    <!--"%d %t %p %l %m %n":

    1、%d输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss},输出类似:2005-7-19 17:49:27,刚好适合插入SQLServer;

    2、%t 产生该日志事件的线程名;

    3、%p 日志的log_level,如DEBUG、WARN或者INFO;

    4、%c 输出所属的类目,通常就是所在类的全名,如"iNotes.Default";

    5、%m 日志的内容;

    6、%l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。如write2database.main(write2database.java:33);

    7、%n 输出一个回车换行符,Windows平台为" ",Unix平台为" "

    -->

                    <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>

                </layout>

            </appender>

            <appender name="AdoNetAppender_SqlServer" type="log4net.Appender.AdoNetAppender">

                <filter type="log4net.Filter.LevelRangeFilter">

                    <levelMin value="Warn" />

                    <levelMax value="Fatal" />

                </filter>

                <bufferSize value="0" />

                <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

                <connectionString value="server=DEVEXPRESSHaoSQL;database=log4net;user id=sa;password=****"/>

                <commandText value="INSERT INTO WebLog_Msg([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.RawTimeStampLayout" />

                </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="255" />

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

            </appender>

        </log4net>

        <!--Log4Net config section end-->

    </configuration>

     

    • 测试

    static void Main(string[] args)

    {

    try

    {

    throw new Exception("LogInfo");

    }

    catch (Exception ex)

    {

    LoggHelper.LogInfo(ex);

    }

     

    try

    {

    throw new Exception("LogWarn");

    }

    catch (Exception ex)

    {

    LoggHelper.LogWarn(ex);

    }

     

    try

    {

    throw new Exception("LogError");

    }

    catch (Exception ex)

    {

    LoggHelper.LogError(ex);

    }

     

    try

    {

    throw new Exception("LogFatal");

    }

    catch (Exception ex)

    {

    LoggHelper.LogFatal(ex);

    }

    Console.ReadLine();

     

    }

     

    截图

  • 相关阅读:
    第一节:RestfulCRUD案例
    第三节:<mvc:annotationdriven />标签解析
    第二节:SpringMVC 的表单标签
    第一节:SpringMVC—数据绑定流程分析
    第四章:(2)Web开发 之 webjars&静态资源映射规则
    第三节:处理静态资源
    10.05
    四则运算二柱子1
    10.08
    10.02
  • 原文地址:https://www.cnblogs.com/hlm750908/p/14864801.html
Copyright © 2020-2023  润新知