c# 添加日志

引用

2.添加配置

  a.在主项目配置文件里

    

  b. 关键

     

3.添加代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace KingUnit.Framework

{

    public class LogProvider

    {

        private static log4net.ILog log = null;

        private LogProvider()

        {

        }

        private static log4net.ILog GetLogger()

        {

            if (log == null)

            {

                log = log4net.LogManager.GetLogger(ConfigProvider.Log4NetAppender);

            }

            return log;

        }

        public static void LogInfo(string message)

        {

            var log = GetLogger();

            log.Info(message);

        }

        public static void LogDebug(string message, Exception exception = null)

        {

            var log = GetLogger();

            log.Debug(message, exception);

        }

        public static void LogWarn(string message, Exception exception = null)

        {

            var log = GetLogger();

            log.Warn(message, exception);

        }

        public static void LogFatal(string message, Exception exception = null)

        {

            var log = GetLogger();

            log.Fatal(message, exception);

        }

        public static void LogError(string message, Exception exception = null)

        {

            var log = GetLogger();

            log.Error(message, exception);

        }

    }

}

1

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

1

2

<add key="log4net.Config.Watch" value="True" />

<add key="Log4NetAppender" value="RollingFile" />

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<log4net>

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

      <file value="log/" />

      <appendToFile value="true" />

      <rollingStyle value="Date" />

      <datePattern value="yyyy-MM-dd".log"" />

      <staticLogFileName value="false" />

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

        <conversionPattern value="[%date]    [%thread]    [%-5level]    [%logger]    -    %message%newline" />

      </layout>

    </appender>

    <root>

      <level value="ALL" />

      <appender-ref ref="RollingFile" />

    </root>

  </log4net>

1

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

   读取配置文件参数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public class ConfigProvider

   {

       public static string Log4NetAppender

       {

           get

           {

               string result = string.Empty;

               try

               {

                   result = ConfigurationManager.AppSettings["Log4NetAppender"];

               }

               catch (Exception ex)

               {

                   LogProvider.LogError(ex.Message);

               }

               return result;

           }

       }

    }

猜你喜欢

转载自blog.csdn.net/yueyangsheng/article/details/82876604