随笔 - 9  文章 - 0 评论 - 6 trackbacks - 0

3.6、GG_Logs 日志类库封装使用说明

GG_Logs类库项目,Nuget安装log4net

 

添加代码配置代码:

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

 

 

 

添加操作类代码(Log4Net.cs):

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using log4net;

 

namespace GG_Logs

{

    public static class Log4Net

    {

        public static void Debug(object message)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);

        }

 

        public static void Debug(object message, Exception ex)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);

        }

 

        public static void Error(object message)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);

        }

 

        public static void Error(object message, Exception exception)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);

        }

 

        private static string GetCurrentMethodFullName()

        {

            try

            {

                StackFrame frame;

                string str2;

                int num = 2;

                StackTrace trace = new StackTrace();

                int length = trace.GetFrames().Length;

                do

                {

                    frame = trace.GetFrame(num++);

                    str2 = frame.GetMethod().DeclaringType.ToString();

                }

                while (str2.EndsWith("Exception") && (num < length));

                string name = frame.GetMethod().Name;

                return (str2 + "." + name);

            }

            catch

            {

                return null;

            }

        }

 

        public static void Info(object message)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);

        }

 

        public static void Info(object message, Exception ex)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);

        }

 

        public static void Warn(object message)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);

        }

 

        public static void Warn(object message, Exception ex)

        {

            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);

        }

 

    }

}

 

 

在使用的项目中引用类的GG_Logs.dll,新建文件log4net.config 并添加配置代码:

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

 

<configuration>

 

  <configSections>

 

    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>

 

  </configSections>

 

  <log4net>

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

    <!-- Set root logger level to ERROR and its appenders -->

    <root>

      <level value="ALL" />

      <appender-ref ref="SysAppender" />

    </root>

    <!-- Print only messages of level DEBUG or above in the packages -->

    <logger name="WebLogger">

      <level value="DEBUG" />

    </logger>

    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">

      <!--文件名称-->

      <appendToFile value="true"/>

      <param name="File" value="C:/Logs/" />

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

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

      <!--文件名称-->

      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />

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

      <!--设置无限备份=-1 ,最大备份数为1000-->

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

      <!--每个文件的大小-->

      <param name="MaximumFileSize" value="500KB"/>

     

     

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

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

      </layout>

    </appender>

    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">

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

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

      </layout>

    </appender>

  </log4net>

 

</configuration>

 

调用代码很简单。

using GG_Logs;

Log4Net.Error("hhhhhhhhhhhhh");

 

实际项目调用,建立mvc模式的web项目,项目名称Web。

 

 

当前mvc项目想创建一个log4net.config 日志配置文件:代码如下

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

 

<configuration>

 

  <configSections>

 

    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>

 

  </configSections>

  <log4net>

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

    <!-- Set root logger level to ERROR and its appenders -->

    <root>

      <level value="ALL" />

      <appender-ref ref="SysAppender" />

    </root>

    <!-- Print only messages of level DEBUG or above in the packages -->

    <logger name="WebLogger">

      <level value="DEBUG" />

    </logger>

    <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net">

      <!--文件名称-->

      <appendToFile value="true"/>

      <param name="File" value="D:/Logs/" />

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

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

      <!--文件名称-->

      <param name="DatePattern" value="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />

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

      <!--设置无限备份=-1 ,最大备份数为1000-->

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

      <!--每个文件的大小-->

      <param name="MaximumFileSize" value="500KB"/>

 

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

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

      </layout>

    </appender>

    <appender name="consoleApp" type="log4net.Appender.ConsoleAppender,log4net">

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

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

      </layout>

    </appender>

  </log4net>

 

</configuration>

 

当前项目添加引用

 

 

 

打开HomeController.cs 控制器 ,添加代码

using GG_Logs;  //命名空间引用

Index()方法加入代码Log4Net.Error("hhhhhhhhhhhhh");

 

运行项目,在D盘logs文件夹下有txt文本,日志操作完成。

 

需要的可加qq群:547765059  ,源码在空间里。

posted on 2019-05-31 16:36 seem111111 阅读(...) 评论(...) 编辑 收藏