Discard this shell.
Configuration
To initialize logging, you can easily configure it in your launch project.
- Click on the arrow next to your βPropertiesβ in your startup project in Solution explorer
- Double click on assembly information

3 Add the following:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
Screenshot:

Using
Now in your classes just add:
public class YourClass { private ILog _logger = LogManager.GetLogger(typeof(YourClass));
And in your log4net.config you can now use the logger property in the output:
<layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-7level %-40logger %message%newline" /> </layout>
What will the namespace and type name print on each line of the log ( -7 and -40 fill the names so that I get straight columns).
Another great thing is that you can also use a filter in the namespace (so that all database classes are registered in "databases.log", etc.).
<appender name="DatabaseAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:\Logs\MyApp\Database.log" /> <rollingStyle value="Composite" /> <datePattern value=".yyyy-MM-dd'.log'" /> <appendToFile value="true" /> <maximumFileSize value="50MB" /> <maxSizeRollBackups value="5" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date % level@ %thread [%logger] %message%newline" /> </layout> <filter type="log4net.Filter.LoggerMatchFilter"> <loggerToMatch value="CompanyName.DatabaseNamespace"/> </filter> <filter type="log4net.Filter.DenyAllFilter" /> </appender>
You can also use %type{1} if %logger get only the class name.
source share