Class will not log to a file with log4net - c#

I've got a MVC based C# Windows Forms application. I've got the following configuration in the app.config file:
<log4net>
<root>
<level value="ALL"/>
<appender-ref ref="LogConsoleAppender"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
<appender name="LogConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="QAC.Source.Services.Log.LogPatternLayout">
<conversionPattern value="%date %level [Thread: #%thread] %message [%logger -> %M]%newline"/>
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs/QAC_"/>
<datePattern value="yyyy-MM-dd_HH-mm-ss'.log'"/>
<staticLogFileName value="false"/>
<appendToFile value="false"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="50"/>
<maximumFileSize value="10MB"/>
<layout type="QAC.Source.Services.Log.LogPatternLayout">
<conversionPattern value="%date %level [Thread: #%thread] %message [%logger -> %M]%newline"/>
</layout>
</appender>
</log4net>
In the main method, in the first lin, I call private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); and after that I initialize my main service:
mainService = MainFactory.getMainServiceInstance();
And there is the problem. All log outputs in the main method are logged successfully to the file and console, also of other services (underlying services from MainService). The only class which is not logged is the MainService. The curiosity at this is, that the logging for this class starts after some time. I don't use any filter. I've turned on the log4net debugging:
<add key="log4net.Internal.Debug" value="true"/>
, but there is no error at least. So I think, that the initialization goes through. Also if I log before I initialize the MainService, it also logs to console and file. Just this class will not log.
Any help would be very nice. Thank you in advance.

I've found the solution. I am using a custom LogPatternLayout which tries also to initialize the MainService and so the MainService is initialized before the LogPatternLayout could be read and initialized into the Logger. Therefore logging takes place after the initialization of the MainService.

Related

How to write text into log4net file

I have a C# application. I would like to integrate a log File. Then I try do this in App.config
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<appender name="InfoFileAppender" type="log4net.Appender.FileAppender">
<file value="info_logfile.txt" />
<appendToFile value="true" />
</appender>
<appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
<file value="error_logfile.txt" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
<level value="INFO" />
<appender-ref ref="InfoFileAppender" />
<level value="ERROR" />
<appender-ref ref="ErrorFileAppender" />
</root>
</log4net>
So I try to write in infoFile:
log.Info("chiamata json");
but I don't see any text in info_logfile.txt
Where is my error?
Can we help me?
Reguards
Do you instantiate your log file? Try the following to see that it works with a console appender. (this example is copied from David Saltner)
using log4net;
using log4net.Config;
public class LogTest
{
private static readonly ILog logger =
LogManager.GetLogger(typeof(LogTest));
static void Main(string[] args)
{
BasicConfigurator.Configure();
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
}
}
.
private static readonly ILog logger =
LogManager.GetLogger(typeof(LogTest));
This creates a logger for the class LogTest. You don't have to use a different logger for each class you have, you can use different loggers for different sections or packages within your code. The class of the logger is output to the log so you know where any logged information has come from.
BasicConfigurator.Configure();
This method initializes the log4net system to use a simple Console appender. Using this allows us to quickly see how log4net works without having to set up different appenders.
This should get you started. Check out this great article for a brief introduction on how to set up appenders and more.
If the configuration is in your app.config file you need to add the sections in the config file:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0,
Culture=neutral, PublicKeyToken=1b44e1d426115821" />
</configSections>
It is better practice to configure your log4.net is a log4net.config file. Then you need to add the following line to your assembly which reads your configuration file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Initialize log4net settings from database

Currently my application (C# Console App) uses file to initialize log4net, is there a way to initialize the log4net configurations from the database ?
I can either save my current file as XML in DB (SQL) but I am not sure how to initialize log4net from that within my application.
EDIT :
Current Implementation:
_logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
var file = new FileInfo(System.Configuration.ConfigurationManager.AppSettings["log4net.Config"]);
log4net.Config.XmlConfigurator.Configure(file);
AppSettings has a location of the physical file which has the following:
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="c:\%property{LogName}" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<staticLogFileName value="false" />
<datePattern value="MM-dd-yyyy'.log'" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level – %message%newline" />
</layout>
</appender>
<root>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
There's an overload to XmlConfigurator.Configure that takes a stream as the parameter - so you just read your config from the database, turn it into a stream, and pass it to that method:
string config = GetConfigFromDb();
using (var MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(config))
{
log4net.Config.XmlConfigurator.Configure(ms);
}

With log4net, do you specify which appender to use?

When you have 2 appenders, say one for debug errors and one for product errors, in your code, do you explicitly create 2 log classes or is an appender basically going to write any log messages that meet a certain criteria?
So in your code, you use a single log method, and depending on how your appenders are setup, it will log the message if it e.g. is logged from a specific namespace, or is a certain log level.
So its possible a single log entry is written to 2 log files?
Yes, you can have single log statements post to multiple appenders. As long as it meets the criteria for each, it will use each appender.
For example, take this config section, it logs all messages to file, and also logs warnings and errors to event viewer.:
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\logs\MySite"/>
<appendToFile value="true"/>
<datePattern value=".yyyy-MM-dd.\l\o\g"/>
<rollingStyle value="Date"/>
<MaxSizeRollBackups value="14"/>
<param name="StaticLogFileName" value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%thread] %-5level %logger{1} - %m%n"/>
</layout>
</appender>
<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
<applicationName value="Trading.Web"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%thread] %-5level %logger{1} - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="WARN"/>
<param name="LevelMax" value="ERROR"/>
</filter>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="EventLogAppender" />
</root>
</log4net>
So this:
_log.Debug("This is a debug message");
Will only appear in the log file (because it doesn't meet the event log appender's filter criteria).
But this:
_log.Error("This is an error message.");
Will log to both the log file and the event viewer.
UPDATE: On your filtering question, if you had:
<root>
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</root>
<!-- Print only messages of level WARN or above in the namespace Com.Foo -->
<logger name="Com.Foo">
<level value="WARN" />
</logger>
Then all things under Com.Foo will log if WARN or higher, but everything else would log at DEBUG or higher....

log4net logging in different files

Two instances of the same program are configured to log in separate files, here is my log4net.config:
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log/svc-%property{ServiceName}"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="'-'yyyy'-'MM'-'dd'.log'"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>
And here's how I instance the logs:
log = LogManager.GetLogger(ServiceName);
GlobalContext.Properties["ServiceName"] = ServiceName;
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
Now, the services are called stuff like "Service1" "Service10" "Service2".
When I start services 1 and 10, the ones I'm testing with, they both create their log files:
svc-BettweenSvc1-2011-07-26.log
svc-BettweenSvc10-2011-07-26.log
But when the second service is started, the logs from the first one start being appended to the second log, and that one is no longer used.
How can I log to separate log files properly in windows services using log4net?
I believe the problem is that you only have one log4net Repository configured (most likely the default one). When you change the log4net log file name, therefore, all loggers start logging to the new file name.
I believe that you will need to configure separate repositories for each separate log file you want active at the same time.
I'm sorry I can't help more, I'm struggling with almost the same issue right now (among other, more pressing, issues).
Good luck!
edit:
check out this question/answer for a possible solution:
how-to-stop-log4net-from-writing-to-two-separate-files
For the record, this works, but it's not what I'm testing against, it still doesn't work in my service, I'll just post it to hopefully help others though:
namespace log4netMultiLogTester
{
public static class Program
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
Process.Start(new ProcessStartInfo("log4netMultiLogTester.exe", "testApp1"));
Process.Start(new ProcessStartInfo("log4netMultiLogTester.exe", "testApp2"));
}
else
{
var logId = args[0];
var log = LogManager.GetLogger(logId);
GlobalContext.Properties["ServiceName"] = logId;
XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
log.Info("Starting log: {0}".FormatWith(logId));
do
{
log.Info("Log: {0}, latest entry is {1}".FormatWith(logId,DateTime.Now.Ticks));
} while (Console.ReadKey().Key != ConsoleKey.R);
}
}
}
public static class StringExtensions
{
public static string FormatWith(this string text,params object[] args)
{
return string.Format(text, args);
}
}
}
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="Log/svc-%property{ServiceName}"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="'-'yyyy'-'MM'-'dd'.log'"/>
<staticLogFileName value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%utcdate] %-5level %logger: %message%newline%exception"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
</root>
</log4net>

log4net not working in dll

I'm currently having issues with getting log4net to work within a particular dll. I'm currently using log4net in other dlls being called by my test app and logging is working fine within those dlls and also within my test app. It's this one particular dll that I'm having trouble with. Here is snippet of code from the dll I'm having trouble with.
//This is from ABC.dll
public class SessionFactory
{
protected static ISessionFactory sessionFactory;
private static readonly ILog log = LogManager.GetLogger(typeof(SessionFactory));
private static void Init()
{
try
{
//Read the configuration from hibernate.xml.cfg or app.config
Configuration normalConfig = new Configuration().Configure();
ConfigureNhibernateValidator(normalConfig);
log.Debug("Initializing session factory");
sessionFactory = Fluently.Configure(normalConfig)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<OrderHeaderMap>()
.Conventions.AddFromAssemblyOf<PascalCaseColumnNameConvention>())
.ProxyFactoryFactory("NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu")
.BuildSessionFactory();
log.Debug("Finished initializing the session factory");
}
catch(Exception ex)
{
//Code not shown
}
}
}
In my test app I am calling:
log4net.Config.XmlConfigurator.Configure();
Here is my log4net configuration in my App.config file:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<!-- ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF -->
<root>
<level value="DEBUG"/>
<appender-ref ref="SpeedTest"/>
</root>
<!-- This is a default logger that nhibernate uses to push out all the SQL statements to-->
<logger name="NHibernate.SQL" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="NHibernateConsoleLog"/>
<appender-ref ref="NHibernateFileLog"/>
</logger>
<!-- This is a default logger that nhibernate uses to push out all the debugging type information to-->
<logger name="NHibernate" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="NHibernateFileLog"/>
</logger>
<appender name="NHibernateConsoleLog" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
</appender>
<appender name="NHibernateFileLog" type="log4net.Appender.RollingFileAppender">
<file value="Logs/nhibernate.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="SpeedTest" type="log4net.Appender.RollingFileAppender">
<file value="Logs/SpeedTest.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%t] %-5p %c - %m%n" />
</layout>
</appender>
</log4net>
</configuration>
Again logging is working fine within my test application using the SpeedTest appender but the logging within the above snippet of code is not working. I set breakpoints above on when it initializes the logger and it seems to hit it. I can post the log4net debug output if necessary but I didn't really see much. Just let me know if you need it and I will post.
Any suggestions as to why logging is not being recorded in the above snippet of code?
It seems that this issue was stemming from me changing the directory to all my external dependencies (log4net being one of them) awhile back in TFS. All I did was drop all my references in my visual studio project and re-add them from my new dependencies folder and everything worked as expected after this. Thanks for all those that helped here.
My suspicion would be that it isn't reading the configuration from the configuration file when you call configure.
If you add the following lines to your application, what do you see (either on the console, or in IDE output window, or by stepping through in the debugger):
var log4netConfig = ConfigurationManager.GetSection("log4net");
var log4netConfigIsNull = log4netConfig == null;
Console.WriteLine(log4netConfigIsNull);
Does it look like the configuration is actually available from the configuration file?
[Edit: in response to your comment]
If you now add another line of debug code to your app:
Console.WriteLine(log.IsDebugEnabled);
what output do you get? Does the logger think it is configured for debug logging? (I'm particularly interested in the behaviour of this in, or after, SessionFactory.Init).
The immediate thought would be that your logging code isn't getting hit, possibly due to an exception being thrown prior to your first log4net call. Can you put a log entry into a finally block, just to test that the logger works?

Categories

Resources