MovGP0    Über mich    Hilfen    Artikel    Weblinks    Literatur    Zitate    Notizen    Programmierung    MSCert    Physik   


Logging

Log Levels

Log4J Log Levels
LevelDescription
OFFThe highest possible rank and is intended to turn off logging.
FATALSevere errors that cause premature termination. Expect these to be immediately visible on a status console.
ERROROther runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
WARNUse of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console.
INFOInteresting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
DEBUGDetailed information on the flow through the system. Expect these to be written to logs only.
TRACEMost detailed information. Expect these to be written to logs only.

NuGet-Pakete

NLoglog4net
  • NLog
  • NLog.Config
  • NLog.Schema
  • log4net
  • log4net.Ext.Json

Konfiguration

NLoglog4net
app.config / web.config
<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>  </configSections>  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">    <include file="NLog.config" />  </nlog>  <appSettings>  </appSettings>
NLog.config
<?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">  <targets>    <target xsi:type="EventLog" name="eventLog" layout="${message}${newline}${exception:format=ToString}" source="${appName}" />  </targets>  <rules>    <logger name="*" minlevel="Error" writeTo="eventLog" />  </rules></nlog>
app.config / web.config
<configuration>  <configSections>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />  </configSections>  <log4net>    <!-- base config -->  </log4net>  <appSettings>     <add key="log4net.Config" value="log4net.config" />  </appSettings>
log4net.config
<log4net debug="true">  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >    <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />    </layout>  </appender>  <root>    <level value="DEBUG" />    <appender-ref ref="EventLogAppender" />  </root></log4net>

Log4Net Debugging

  <system.diagnostics>    <trace autoflush="true">      <listeners>        <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="%TEMP%\log.txt" />      </listeners>    </trace>  </system.diagnostics>  <appSettings>    <add key="log4net.Internal.Debug" value="true"/>  </appSettings>


Common Logging

  <configSections>    <sectionGroup name="common">      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />    </sectionGroup>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />    <!-- ... -->  </configSection>  <!-- ... -->  <common>    <logging>      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">        choices are INLINE, FILE, FILE-WATCH, EXTERNAL        otherwise BasicConfigurer.Configure is used        log4net configuration file is specified with key configFile        <arg key="configType" value="FILE-WATCH" />        <arg key="configFile" value="~/Config/log4net.config"/>        <arg key="level" value="INFO" />      </factoryAdapter>    </logging>  </common>  <!-- ... -->

Bootstrapping

Bootstrapping von log4net
log4net
in Global.asax
protected void Application_Start(Object sender, EventArgs e){   log4net.Config.XmlConfigurator.Configure();}
oder AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Manuelles Laden der Konfiguration
(falls kein Zugriff auf web.config)
NLoglog4net
using(var config = Assembly.GetExecutingAssembly().GetManifestResourceStream("nlog.config")){   var reader = XmlReader.Create(config);   NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(reader, true));}
using(var config = Assembly.GetExecutingAssembly().GetManifestResourceStream("log4net.config")){   XmlConfigurator.Configure(config);}

PostSharp-Aspekt

NLogLog4Net
using NLog;using PostSharp.Aspects;using System;namespace .....{    [Serializable]    public class LogAttribute : OnMethodBoundaryAspect    {        [NonSerialized]        private Logger logger;        public LogAttribute()        {            logger = LogManager.GetCurrentClassLogger();        }        public override void OnEntry(MethodExecutionArgs args)        {            logger.Trace(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));        }        public override void OnExit(MethodExecutionArgs args)        {            logger.Trace(string.Format("Leaving {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));        }        public override void OnException(MethodExecutionArgs args)        {            logger.Error(args.Exception.Message, args.Exception);        }    }}
using NLog;using PostSharp.Aspects;using System;namespace .....{    [Serializable]    public class LogAttribute : OnMethodBoundaryAspect    {        [NonSerialized]        private ILog logger;        public LogAttribute()        {            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);        }        public override void OnEntry(MethodExecutionArgs args)        {            logger.Trace(string.Format("Entering {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));        }        public override void OnExit(MethodExecutionArgs args)        {            logger.Trace(string.Format("Leaving {0}.{1}.", args.Method.DeclaringType.Name, args.Method.Name));        }        public override void OnException(MethodExecutionArgs args)        {            logger.Error(args.Exception.Message, args.Exception);        }    }}

Global Exception Logging

Console Application
static void Main() {    AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;    // ...}
ASP.NET Application