<?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="AsyncWrapper" queueLimit="5000" overflowAction="Discard"> <target xsi:type="File" fileName="${basedir}/logs/${Logger}/${shortdate}.log" layout="${level}--------------------${longdate}${newline}${message}${newline}" /> </target> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="file" /> </rules> </nlog>
public static class LogUtil { static LogUtil() { NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(Path.GetDirectoryName(typeof(LogUtil).Assembly.Location) + "\nlog.config"); } 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); } }