How to specify common application data folder for log4net?

We just use this: <param name=”File” value=”${ALLUSERSPROFILE}/Company/Product/Logs/error.log”/> It works great. This line can simply be inserted into your current appender configuration: <appender name=”RollingFileAppender” type=”log4net.Appender.RollingFileAppender”> <param name=”File” value=”${ALLUSERSPROFILE}/Company/Product/Logs/error.log”/> </appender>

how to convert my decimal thread ID to hex and make it appear in hex format in log4net conversion pattern?

You could write a converter like this: public sealed class HexPatternConverter : PatternLayoutConverter { override protected void Convert(TextWriter writer, LoggingEvent loggingEvent) { long id; if (long.TryParse(loggingEvent.ThreadName, out id)) { writer.Write(id.ToString(“X”)); } else { writer.Write(loggingEvent.ThreadName); } } } then you configure the layout like this: <layout type=”log4net.Layout.PatternLayout”> <conversionPattern value=”[%hex_thread] %message%newline” /> <converter> <name value=”hex_thread” /> <type … Read more

log4net: Configure to ignore messages from a specific class

Sure, use a filter. Here’s the snippet posted on the blog, for future reference – all credit to the author of that blog post: <filter type=”log4net.Filter.LoggerMatchFilter”> <!– allows this sub-namespace to be logged… –> <loggerToMatch value=”Noisy.Namespace.But.Important” /> </filter> <filter type=”log4net.Filter.LoggerMatchFilter”> <!– …but not the rest of it –> <loggerToMatch value=”Noisy.Namespace” /> <acceptOnMatch value=”false” /> </filter>

How to log Trace messages with log4net?

According to Rune’s suggestion I implemented a basic TraceListener which output to log4net: public class Log4netTraceListener : System.Diagnostics.TraceListener { private readonly log4net.ILog _log; public Log4netTraceListener() { _log = log4net.LogManager.GetLogger(“System.Diagnostics.Redirection”); } public Log4netTraceListener(log4net.ILog log) { _log = log; } public override void Write(string message) { if (_log != null) { _log.Debug(message); } } public override void … Read more

Enable file logging for log4net from code instead of from configuration

I always use the code below to configure log4net from code. Works great! Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Root.RemoveAllAppenders(); /*Remove any other appenders*/ FileAppender fileAppender = new FileAppender(); fileAppender.AppendToFile = true; fileAppender.LockingModel = new FileAppender.MinimalLock(); fileAppender.File = Server.MapPath(“https://stackoverflow.com/”) + “log.txt”; PatternLayout pl = new PatternLayout(); pl.ConversionPattern = “%d [%2%t] %-5p [%-10c] %m%n%n”; pl.ActivateOptions(); fileAppender.Layout = pl; … Read more

How to log stack trace using log4net (C#)

Use this: void Error(object message,Exception t) Reason is in log4net documentation for void Error(object message): WARNING Note that passing an Exception to this method will print the name of the Exception but no stack trace. To print a stack trace use the void Error(object,Exception) form instead. Error(object message, Exception t) is the most flexible way … Read more

How to use Property Injection with AutoFac?

In my opinion the solution Ninject created is much nicer than the propertyinjection in Autofac. Therefore I created a a custom attribute which is a postsharp aspect which automatically injects my classes: [AutofacResolve] public IStorageManager StorageManager { get; set; } My aspect: [Serializable] [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class AutofacResolveAttribute : LocationInterceptionAspect { public override … Read more