ILog logger = LogManager.GetLogger(this.GetType()); logger.Debug(string.format("Entering method x(parm1: {0}, parm2: {1}", parm1, parm2)); try { <Application code> logger.Debug(string.format("Exiting method x. Return {0}", returnValue)); catch (Exception err) { logger.Error("Method x threw exception", err); throw; }
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <log4net> <appender name="LogToFile" type="log4net.Appender.FileAppender"> <file value="<Filename for Your Log>"/> <appendToFile value="false"/> <layout type="log4net.Layout.PatternLayout"> <!-- You can change the pattern to include caller information and other stuff. Do an Internet search on log4net pattern layout for more information. --> <conversionPattern value="%n%d %-5level %logger%n%m%n"/> </layout> </appender> <root> <!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF --> <level value="ERROR"/> <appender-ref ref="LogToFile"/> </root> </configuration>
using System.Reflection; using log4net.Config; using log4net.Core; using SharpArchContrib.Core.Logging; using SharpArchContrib.PostSharp.Logging; <other assembly-level attributes such as AssemblyCompany and AssemblyCopyright (if using CommonAssemblyInfo.cs)> [assembly: Log(AttributeTargetTypes = "*", AttributePriority = 1, EntryLevel = LoggingLevel.Debug, SuccessLevel = LoggingLevel.Debug, ExceptionLevel = LoggingLevel.Error)] [assembly: XmlConfigurator(Watch = true)]
using System.Reflection; using log4net.Config; using log4net.Core; using SharpArchContrib.Core.Logging; using SharpArchContrib.PostSharp.Logging; <other assembly-level attributes such as AssemblyCompany and AssemblyCopyright (if using CommonAssemblyInfo.cs> [assembly: Log(AttributeTargetTypes = "*")] [assembly: XmlConfigurator(Watch = true)]
[Exception(IsSilent = true, ReturnValue = false)] public bool DoIt() { //do some stuff that could fail return true; }