Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The code below shows how you can programatically configure log4Net without using a configuration file to achieve the effect you're looking for. Basically, it just involves creating a named logger and adding to the hierarchy.</p> <p>I used as a starting point one of the answers from <a href="https://stackoverflow.com/questions/769983/how-to-configure-log4net-programmatically-from-scratch-no-config">here</a>.</p> <pre><code>using log4net; using log4net.Appender; using log4net.Layout; using log4net.Repository.Hierarchy; namespace LoggerTest { class Program { static void Main(string[] args) { DeviceConnection dev1 = new DeviceConnection("Device1"); DeviceConnection dev2 = new DeviceConnection("Device2"); dev1.DoSomething(); dev2.DoSomething(); } } public class DeviceConnection { private string name; private readonly ILog logger; public DeviceConnection(string _name) { name = _name; logger = TestLogger.AddNamedLogger(name); logger.Info("---- Begin Logging for DeviceConnection: " + name); } public void DoSomething() { logger.Info("Doing something for device connection " + name); } } public static class TestLogger { private static PatternLayout _layout = new PatternLayout(); private const string LOG_PATTERN = "%d [%t] %-5p %m%n"; public static string DefaultPattern { get { return LOG_PATTERN; } } static TestLogger() { _layout.ConversionPattern = DefaultPattern; _layout.ActivateOptions(); Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); hierarchy.Configured = true; } public static PatternLayout DefaultLayout { get { return _layout; } } public static ILog AddNamedLogger(string name) { Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); Logger newLogger = hierarchy.GetLogger(name) as Logger; PatternLayout patternLayout = new PatternLayout(); patternLayout.ConversionPattern = LOG_PATTERN; patternLayout.ActivateOptions(); RollingFileAppender roller = new RollingFileAppender(); roller.Layout = patternLayout; roller.AppendToFile = true; roller.RollingStyle = RollingFileAppender.RollingMode.Size; roller.MaxSizeRollBackups = 4; roller.MaximumFileSize = "100KB"; roller.StaticLogFileName = true; roller.File = name + ".log"; roller.ActivateOptions(); newLogger.AddAppender(roller); return LogManager.GetLogger(name); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload