Ale po co Ci te rzeczy?
Klepiesz konfiguracje loggera, która w podstawowej formie może wyglądać na przykład tak:
public class LoggerConfig
{
public static void Register()
{
var config = new LoggingConfiguration();
var fileTarget = new FileTarget();
fileTarget.Layout = GetLayout();
fileTarget.FileName = "${basedir}/../logs/${date:format=yyyy-MM-dd}.txt";
config.AddTarget("file", fileTarget);
var rule = new LoggingRule("*", LogLevel.Trace, fileTarget);
config.LoggingRules.Add(rule);
LogManager.Configuration = config;
}
private static string GetLayout()
{
var nl = Environment.NewLine;
var date = @"${longdate}";
var message = @"${message}";
var stacktrace = @"${stacktrace:format=DetailedFlat:topFrames=30:separator=" + nl + "}";
return string.Format("[{0}]: {1}{3}{3}{2}{3}{3}", date, message, stacktrace, nl);
}
}
Wywołujesz gdzieś w main
metode Register()
, a potem używasz na przykład tak:
public class CustomHandleErrorAttribute : HandleErrorAttribute
{
private Logger Logger { get { return LogManager.GetCurrentClassLogger(); } }
public override void OnException(ExceptionContext filterContext)
{
Logger.Error(filterContext.Exception.Message, filterContext.Exception);
base.OnException(filterContext);
}
}
Cała filozofia.