Get Appenders from Log4Net LogManager - c#

I'm trying to get name of the file with log by log4net, but I can't get my Appenders. This is my config:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="name.log"/>
<appendToFile value="false"/>
<maximumFileSize value="10MB"/>
<maxSizeRollBackups value="0"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline"/>
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %thread %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>
</log4net>
This is the code for getting count of appenders:
((Hierarchy)LogManager.GetRepository()).GetAppenders().Length.ToString();
But it gives me 0.
Logging works perfectly, so config works for logging. Do you have any ideas how to get the appenders or file name "name.log" from config without appenders? Thank you.

Is it possible you're fetching the appenders before your call to Configure?
Using the config you provided:
int count;
count = LogManager.GetRepository().GetAppenders().Length; // 0
XmlConfigurator.Configure();
count = LogManager.GetRepository().GetAppenders().Length; // 2

Related

Two log4net configuration files on same solution

I have an solution which consist of multiple projects.its a desktop application I need to have two separate log4net configurations for those projects since one project contains a library that uses log4net itself. On one project log4net configured in App.config and other project it is configured in log4net.configuration file.
below is one configuration file
'''
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="SmtpAppender"/>
<appender-ref ref="LogFileAppender"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\seperatelog.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Date" />
<datePattern value="'On_'yyyy-MM-dd'.log'" />
<appendToFile value="true" />
<maxSizeRollBackups value="14" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="bobby.chopra#prcm.com"/>
<from value="TagFileUploader#prcm.com"/>
<subject value="TagFileUploader ERROR"/>
<smtpHost value="prc-mn-ex01"/>
<bufferSize value="512"/>
<lossy value="true"/>
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
<appender name="cargillsJournal" type="log4net.Appender.FileAppender">
<param name="File" value="D:\BankInABoxffffff-log.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Date" />
<datePattern value="'On_'yyyy-MM-dd'.log'" />
<appendToFile value="true" />
<maxSizeRollBackups value="14" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline" />
</layout>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger: %message%newline"/>
</layout>
</appender>
</log4net>
Other configuration file is below
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="console" />
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="rolling-log.txt" />
<appendToFile value="true" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100" />
<rollingStyle value="Size" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<header value="[Header]
" />
<footer value="[Footer]
" />
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
</layout>
</appender>
<logger name="LoggingExample">
<!-- <appender-ref ref="B" /> -->
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
those two log file load correctly but they all write on to the same log file seperatelog.txt
What i need is to have two separate files to create for those 2 configurations.
Seems you'll better define log4net configuration not in app.config but in dedicated XML files. Names of XML files can be composed according to some convention so your projects will know what log4net configuration XML is dedicated to each one.
You'll able to configure your log4net with XmlConfigurator.Configure(...) function

log4net is not writing any messages to the log file or the console

So, I have a console app.
Using log4net to log messages.
Below is my log4net.config file.
<?xml version="1.0"?>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="White" />
<backColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="White" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
And in my console app I have this class to run
public class SchedulerService
{
private static readonly ILog _log = LogManager.GetLogger(typeof(SchedulerService));
private readonly SchedulerRegistry _registry;
public SchedulerService(SchedulerRegistry schedulerRegistry)
{
_registry = schedulerRegistry;
}
public void Start()
{
JobManager.Initialize(_registry);
JobManager.JobException += JobManager_JobException;
_log.Info("SchedulerService is started");
}
private void JobManager_JobException(JobExceptionInfo info)
{
Console.WriteLine("An error just happened with a scheduled job: " + info.Exception);
_log.Error("An error just happened with a scheduled job: " + info.Exception);
}
public void Stop()
{
Console.WriteLine("SchedulerService is stopped");
}
}
As a result log.txt file is created but its size is zero, its empty, nothing is logged into that files and nothing is logged to console.
What am I doing wrong?
Thanks a lot.
Your log4net.config is correct.
Make sure:
The Copy to Output Directory property of the log4net.config file is set to Copy Always. This is important because you need the config file to be copied to the bin folder when you build and run your application. Do this by right clicking the config file and select Properties. Then set the value of the Copy to Output Directory property to Copy Always.
You tell log4net to load your config. One way to achieve this is to add the following line to the bottom of your AssemblyInfo file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
Found the problem :)
Looks like its a new feature of VS 2017 that I haven't noticed before.
Previously you could resolve namespace only if you added corresponding components yourself manually via Add Reference or via NuGet Package Manager.
With VS 2017 when you try to resolve a namespace, it actually installs these packages for you. This maybe convenient, but also frustrating, like in my case.
Don't know why but VS decided to include this Common.Logging & Common.Logging.Core packages instead of log4net that was already in my solution.
Probably I trusted blindly and didn't notice that these packages have nothing to do with the library I wanted to use. The weird thing is that the code compiled just fine :)
So that was the reason why the code wasn't working. Be aware.
Try this method. You have to name it ColoredConsoleAppender instead of ConsoleAppender.Hope it works.
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="log.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR"/>
<foreColor value="Red"/>
</mapping>
<mapping>
<level value="INFO"/>
<foreColor value="White"/>
</mapping>
<mapping>
<level value="DEBUG"/>
<foreColor value="Green"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message%newline"/>
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<priority value="ALL"/>
<appender-ref ref="FileAppender"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>

See debug messages of log4net in immediate window

I have an application that uses log4net. I dump debug to a file as well as stdout.
When launching the application normally, I see all the messages in the output section as well as in the file.
If I create a class/ run a function that writes something to the log in the immediate window, I do not see anything in the Output nor in the Immediate window. I do see the log in the file though.
Is there any way I could fix it so I will be able to see these messages in the Immediate window?
Log4net configuration:
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="FileAppender"/>
<appender-ref ref="ConsoleAppender"/>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline"/>
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs/log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="1" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline"/>
</layout>
</appender>
</root>
</log4net>
Add a DebugAppender to your configuration in order to have messages appear in the Immediate window (I tested this on VS2013)
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="DebugAppender" />
</root>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{dd.MM.yyyy HH:mm:ss.ffff} [%thread] %level %logger%exception - %message%newline" />
</layout>
</appender>

log4net outputting file but not to debug window

I'm trying to get output from for errors to show up both in an appended logfile but also in the debug window. The logfile stuff is working correctly, but nothing every shows up in the debug window.
First, here is my config
<log4net>
<!--
This writes the log information to the console window. It only logs events
that are at least at the INFO level (which would mean that DEBUG events are not
captured.
-->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG"/>
<levelMax value="FATAL"/>
</filter>
</appender>
<!--
This stores information in the logfile.txt file. It only captures log events
that contain the key word test or error.
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\TOPS\LOGFILES\logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
and then i call log4net in my code
log.Debug("Debug error logging", ex);
log.Info("Info error logging", ex);
log.Warn("Warn error logging", ex);
log.Error("Error error logging", ex);
log.Fatal("Fatal error logging", ex);
this creates 5 entries in my logfile, but no entries in my console window.
i have also modified my web.config so the appenders were as similar as possible, but i still get the exact same results
<log4net>
<!--
This writes the log information to the console window. It only logs events
that are at least at the INFO level (which would mean that DEBUG events are not
captured.
-->
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<!--
This stores information in the logfile.txt file. It only captures log events
that contain the key word test or error.
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\TOPS\LOGFILES\logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
any ideas on what i may have missed that is breaking the console output?
EDIT:
found the solution, as pointed out in the comments below by making3. i confused console appender and trace appender, which is what i was actually looking for. here's the updated config.
<log4net>
<!--
This writes the log information to the console window. It only logs events
that are at least at the INFO level (which would mean that DEBUG events are not
captured.
-->
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<!--
This stores information in the logfile.txt file. It only captures log events
that contain the key word test or error.
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\TOPS\LOGFILES\logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="TraceAppender"/>
</root>
</log4net>
found the solution, as pointed out in the comments below by making3. i confused console appender and trace appender, which is what i was actually looking for. here's the updated config.
<log4net>
<!--
This writes the log information to the console window. It only logs events
that are at least at the INFO level (which would mean that DEBUG events are not
captured.
-->
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<!--
This stores information in the logfile.txt file. It only captures log events
that contain the key word test or error.
-->
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\TOPS\LOGFILES\logfile.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="5"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="test"/>
</filter>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="error"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="TraceAppender"/>
</root>
</log4net>

Set log level for Gallio execution log

How do I configure log4net or my Gallio test runners (Icarus and Echo) to use a different logging level for the Gallio Execution Log and my own file appender. I want my log file to have a DEBUG log level and Gallio to have an ERROR log level.
I thought this would work but Gallio is reading both appenders:
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="File" type="log4net.Appender.FileAppender">
<file value="test.log" />
<appendToFile value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<logger name="TestNamespace">
<level value="DEBUG" />
<appender-ref ref="File" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="Console" />
</root>
</log4net>
Gallio is redirecting console output to its Execution Log. So I needed to set the threshold of the ConsoleAppender to the level I want for the Gallio Execution Log.
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="ERROR"/>
<layout type="log4net.Layout.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<appender name="File" type="log4net.Appender.FileAppender">
<threshold value="DEBUG"/>
<file value="test.log" />
<appendToFile value="false" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="Console" />
<appender-ref ref="File" />
</root>
</log4net>

Categories