NLog

 
    public static class LogUtil
    {
        static LogUtil()
        {
            NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.Combine(Path.GetDirectoryName(typeof(LogUtil).Assembly.Location), "nlog.conf"));
        }

        private static NLog.ILogger GetLogger(string loggerName)
        {
            return NLog.LogManager.GetLogger(loggerName);
        }

        private static NLog.ILogger _normalLogger = GetLogger("normal");
        private static NLog.ILogger _errorLogger = GetLogger("exception");

        public static void LogDebug(this string msg)
        {
            _normalLogger.Debug(msg);
        }

        public static void LogInfo(this string info)
        {
            _normalLogger.Info(info);
        }

        public static void LogWarn(this string warn)
        {
            _normalLogger.Warn(warn);
        }

        public static void LogError(this Exception error, string extendMsg="")
        {
            _errorLogger.Error(extendMsg + Environment.NewLine + error.Message + Environment.NewLine + error.StackTrace);
        }


    }

 

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true">
    <targets>
        <target name="file" xsi:type="File" fileName="${basedir}/logs/${Logger}/${shortdate}.log"
                layout="${level}--------------------${longdate}${newline}${message}${newline}" />
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

 

posted on 2020-09-17 17:51  jonney_wang  阅读(228)  评论(0编辑  收藏  举报

导航