ASP.NET Core 使用Log4net

1. Nuget安装log4net,图里的两个

2.项目根目录下添加log4net.config.添加下面的代码:

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

  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Log/" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <staticLogFileName value="false" />
      <datePattern value="yyyy-MM-dd'.log'" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date  [%thread]  %-5level  %message%newline" />
      </layout>
    </appender>

    <!-- Setup the root category, add the appenders and set the default level -->
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>

  </log4net>

</configuration>

3. 添加一个logHelper.cs

    public class LogHelper
    {
        private static readonly ILog logger;
        static LogHelper()
        {
            if (logger == null)
            {
                var repository = LogManager.CreateRepository("NETCoreRepository");

                XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));

                logger = LogManager.GetLogger(repository.Name, "InfoLogger");
            }
        }

        /// <summary>
        /// 普通日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Info(string message, Exception exception = null)
        {
            if (exception == null)
                logger.Info(message);
            else
                logger.Info(message, exception);
        }

        /// <summary>
        /// 告警日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Warn(string message, Exception exception = null)
        {
            if (exception == null)
                logger.Warn(message);
            else
                logger.Warn(message, exception);
        }

        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        public static void Error(string message, Exception exception = null)
        {
            if (exception == null)
                logger.Error(message);
            else
                logger.Error(message, exception);
        }
    }

4. 调用:

LogHelper.Info("test Info log");     //普通日志
LogHelper.Warn("test Warn log");      //告警日志
LogHelper.Error("test Error log");   //错误日志

项目里就会生成log文件

可以用来输出SQL:

public class DbContext
    {
        private static SqlSugarClient _db = null;
        /// <summary>
        /// test是数据库名
        /// </summary>
        public static string ConnectionString = "server=localhost;uid=aaa;pwd=123;database=test_db";
        public SqlSugarClient CreateClient()
        {
            _db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConnectionString, //数据库连接字符串
                DbType = DbType.MySql, //必填
                IsAutoCloseConnection = true, //默认false
                InitKeyType = InitKeyType.Attribute,
                AopEvents = new AopEvents
                {
                    OnLogExecuting = (sql, pars) =>
                    {
                        LogHelper.Info("执行SQL: " + sql);
                        if (pars!=null)
                        {
                            LogHelper.Info("Parameter: ");
                            foreach (var s in pars)
                            {
                                LogHelper.Info(string.Format("{0}:{1};", s.ParameterName, s.Value));
                            }
                        }
                    }
                }
            });
            return _db;
        }
    }

相关推荐

  1. 关于 log4net 日志功能使用方法

    2024-07-09 22:00:03       31 阅读
  2. .NET 7(C#)配置使用log4net日志框架的方法

    2024-07-09 22:00:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 22:00:03       3 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 22:00:03       3 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 22:00:03       2 阅读
  4. Python语言-面向对象

    2024-07-09 22:00:03       2 阅读

热门阅读

  1. HTML最快速最简单

    2024-07-09 22:00:03       9 阅读
  2. 等保测评推动黑龙江数字化转型中的安全保障

    2024-07-09 22:00:03       8 阅读
  3. 嵌入式硬件电路常用设计软件

    2024-07-09 22:00:03       9 阅读
  4. [终端安全]-2 移动终端之硬件安全(SE)

    2024-07-09 22:00:03       9 阅读
  5. Kafka 典型问题与排查以及相关优化

    2024-07-09 22:00:03       8 阅读
  6. SQL中字符串类型中char和varchar之间的区别是什么

    2024-07-09 22:00:03       7 阅读
  7. 【框架】ABP(ASP.NET Boilerplate Project)

    2024-07-09 22:00:03       7 阅读