Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, October 14, 2010 2:50 PM
Hi,
I want to use log4net to logs different comments / errors in a file. But, it is not working at all on my system. Even, I'm not facing any error.
Here's my code
Log4Net.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="E:\\Log\\mailin" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<param name="StaticLogFileName" value="false" />
<datePattern value="-yyyy-MM-dd.\l\o\g" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<logger name="MailIn">
<level value="INFO" />
<appender-ref ref="RollingLogFileAppender" />
</logger>
</log4net>
</configuration>
Class.cs
using log4net;
using log4net.Config;
private static ILog log = LogManager.GetLogger("MailIn");
log.Info(String.Format("Log Created"));
AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Log4Net.xml", Watch = true)]
Any idea, let me know
Thanks
Sohaib Khan | http://muhskhac.spaces.live.com
All replies (6)
Thursday, October 14, 2010 7:20 PM âś…Answered
Hi,
Log4Net is designed to be robust and isn't throwing exception.
I tested your config file and it's ok, unless the path e:\log\ isn't accessible/writable (in such case you'll get a warning in VS ouput window).
Did you had your config file to your solution? In such case it'll be search in executable folder but it'll be in your project folder. You might have to put a relative/full path in order to have it loaded correctly. You can also copy it to the exe folder.
I tried with following line:
[assembly
: log4net.Config.XmlConfigurator(ConfigFile = @"..\..\Log4Net.xml"
, Watch = true)]
since my exe was in bin\debug\ folder. It was OK!
kind regards,
Kind regards, Sebastien G. - Axiomtek Deutschland GmbH
Friday, October 15, 2010 1:29 PM
Thanks Sebastian for your response.
Following line helps to me as well.
[assembly
: log4net.Config.XmlConfigurator(ConfigFile = @"..\\.\Log4Net.xml"
, Watch = true)]
I create a project and add another new project under it. That sub project contains log4net approach. log4net approach doesn't work here. But, if i create a seperate project and apply log4net then it works. Any reason ?
My project hierarchy as follows:
Main Project
- Main Project Solution File
- Sub Project
- - Sub Project File
- - log4net.xml
Through above hierarchy, it is not working.
Can you please let me know where should i place log4net.xml ?
Thanks
Sohaib Khan | http://muhskhac.spaces.live.com
Friday, October 15, 2010 2:08 PM
I always choose not to hardcode path. It's a mess when reaching the distribution stage.
Best solution is to keep it in your source, to remove the path in [assembly
: log4net.Config.XmlConfigurator(ConfigFile = @"..\\.\Log4Net.xml"
, Watch = true)]
and to change "Copy to output directory" property for that file to "Copy Always"
I would have think about that solution earlier, sorry!
For your new sub project the problem is probably one more time question of configuration file access.
Your sub project is a dll or an exe? where/how is it launched?
Kind regards, Sebastien G. - Axiomtek Deutschland GmbH
Monday, October 18, 2010 10:16 AM
my sub projects are Class Library, Console & Windows Service.
I'm using log4NET for Console and Windows Service. Both runs seperately (basically, an option for user either to run console or windows service)
Sohaib Khan | http://muhskhac.spaces.live.com
Tuesday, October 19, 2010 2:22 AM
Hi Sohaib,
Welcome to MSDN forums!
Here are some links about Log4net for console application and class library:
http://www.codeproject.com/KB/trace/log4net_intro.aspx
http://remy.supertext.ch/2009/02/using-log4net-for-a-windows-console-application/
http://logging.apache.org/log4net/release/manual/configuration.html
Hope these helps, if you have any problems, please feel free to let me know.
Best Regards,
Alan Chen
________________________________________
Please remember to mark the replies as answers if they help and unmark them if they provide no help
Thursday, November 22, 2012 7:29 AM
Be sure that the MIIS/ILM/FIM sync engine service account has rights to write files in the forlder you specified for the log files in the log4net config.