简单使用log4net

1.用nuget安装log4net

2.在.config文件中写入配置


    
3.在AssemblyInfo.cs文件中加入
[assembly: log4net.Config.XmlConfigurator(Watch = true)] 
4.新建LogHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cooee.Game.LibDaemon
{
    public class LogHelper
    {
        /// 
        /// 输出日志到Log4Net
        /// 
        /// 
        /// 
        #region static void WriteLog(Type t, Exception ex)

        public static void WriteLog(Type t, Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error("Error", ex);
        }

        #endregion

        /// 
        /// 输出日志到Log4Net
        /// 
        /// 
        /// 
        #region static void WriteLog(Type t, string msg)

        public static void WriteLog(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error(msg);
        }

        #endregion
    }
}

5.使用LogHelper.WriteLog(typeof(类名), ex);打印log


你可能感兴趣的:(c#)