Log4Net logger creating a file when it does not have FileAppender - log4net

I've set up log4net to potentially log to three locations: console, rolling file, and a database. I need to be able to turn on and off these three separate locations individually, so I've created a set of loggers, one for each combination. For example...
<logger name="Logfile">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</logger>
<logger name="Console">
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
</logger>
<logger name="LogfileConsole">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
My root logging level is set to OFF with no appenders used so that the default is no logging whatsoever. HOWEVER... when I run one of the projects that has logging set to the root, meaning it is turned off, a log file still gets created. The file remains empty and no logging is performed, but if I'm using a logger without the RollingFileAppender in it, why is it creating the log file?
I thought it might be because the root was totally empty, so I made a logger called "None" and set the level to OFF and gave it the ConsoleAppender just for appearances sake, and it still created the file.
It's not interfering with the running of the project or impacting performance, but I'd prefer my log folder not end up cluttered with empty log files for apps that shouldn't be logging. I CANNOT figure out why it's creating a file when the current logger does not use file appender. Help?

Related

Log4Net at Runtime with Logger not Root

I have a working logger from runtime without using a web.config. I want to do this because I am adding my logger to a DLL I will be distributing. Now I have working version using hierarchy but that is using Root. If I use the web config way of configuring it I can use the following nodes....
<logger name="EventLogger">
<level value="ERROR" />
<appender-ref ref="EventLogAppender" />
</logger>
<logger name="FileLogger">
<level value="INFO" />
<appender-ref ref="FileAppender" />
</logger>
This allows me to call each log when I want to use it. Basically I want to do an info log for the fileappender and eventlog for errors....I then will call them on demand based on what error is generated, like catch errors will be going to eventviewer and specific log calls will be going to fileappender...does anyone have any good runtime code for log4net? I have seen a lot of examples and they all are for root loggers.
This is basically for my dll to allow users to have logs for specific items, errors and logging. Can anyone explain exactly what the MemoryAppender means...

Log4net multiple loggers - File logger not working

Following configuration logs to Eventlog and it works for SmtpSender. But the filelogger does not log, it creates the log file in the path, but does not do any debug logs.
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
But when I change the root logger to RollingFileAppender it logs to file,
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
Any idea why this happens? How can I get file logger working in this scenario.
You did not post the appender confuriguration, however the easiest way to figure out what goes wrong is enable internal debugging. This will tell you what goes wrong in the rolling file appender:
There are 2 different ways to enable internal debugging in log4net.
These are listed below. The preferred method is to specify the
log4net.Internal.Debug option in the application's config file.
• Internal debugging can also be enabled by setting a value in the
application's configuration file (not the log4net configuration file,
unless the log4net config data is embedded in the application's config
file). The log4net.Internal.Debug application setting must be set to
the value true. For example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
This setting is read immediately on startup an will cause all internal
debugging messages to be emitted.
• To enable log4net's internal debug programmatically you need to set
the log4net.Util.LogLog.InternalDebugging property to true. Obviously
the sooner this is set the more debug will be produced.
Internal debugging messages are written to the console and to the
System.Diagnostics.Trace system. If the application does not have a
console the messages logged there will be lost. Note that an
application can redirect the console stream by setting the
System.Console.Out. The Trace system will by default send the message
to an attached debugger (where the messages will appear in the output
window). If the process does not have a debugger attached then the
messages are sent to the system debugger. A utility like DebugView
from http://www.sysinternals.com may be used to capture these
messages.
As log4net internal debug messages are written to the
System.Diagnostics.Trace system it is possible to redirect those
messages to a local file. You can define a trace listener by adding
the following to your application's .config file:
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
Make sure that the process running your application has permission to write to this file.
log4net faq
I managed to get the logging work, in fact I did this as an experiment and it worked.
Added RollingFileAppender to the root element,
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="RollingFileAppender"/>
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>

getting duplicate Log4Net entries

I am getting duplicate lines in my log output with the following web.config entries:
<root>
<priority value="Off"/>
<appender-ref ref="FileAppender"/>
</root>
<logger name="SessionMgr">
<priority value="ALL" />
<appender-ref ref="FileAppender" />
</logger>
If I take the element out I get nothing, as I expect. But, adding the element results in ever line duplicated.
What am I not understanding?
I would try this instead:
<root>
<priority value="Off"/>
<appender-ref ref="FileAppender"/>
</root>
<logger name="SessionMgr">
<priority value="ALL" />
</logger>
Your root logger already tells you to log to FileAppender. You shouldn't need to add it in your SessionMgr logger. You can also see this article for more details:
Eliminate duplicate logging in log4net

Log4net: separate log files for each class

i want to log messages from multiple classes. the problem is that currently i can log all messages into single file.i want to do this class A should log its messages into ALog.txt and class B should log its messages into BLog.txt
please tell me the sample config file for these settings
In addition to the root logger, you can configure individual loggers with their own appenders. Assuming you are calling your loggers ClassA and ClassB you can do:
<root>
<appender-ref ref="CommonAppender" />
</root>
<logger name="ClassA">
<appender-ref ref="AppenderA" />
</logger>
<logger name="ClassB">
<appender-ref ref="AppenderB" />
</logger>
This is further described here.

How to configure a child logger with a lower loglevel than the root level in Log4net

The app I'm working on uses a library that generates a lot of INFO level messages I don't want to log. But the rest of the app and libraries produce INFO level messages I need to log.
How can I setup log4net so that all INFO messages are logged except for one logger that need to log only at WARN or above ? All messages need to be logged in the same files.
Julien
<root>
<level value="INFO" />
<appender-ref ref="someappender" />
</root>
<logger name="AnotherLogger">
<level value="WARN" />
</logger>

Resources