Jeegnesh Sheth
 |
| 10/03/2008 3:57 PM |
|
Hi,
I have a logging DLL which uses log4net to log to a database. I have c# application which calls this logging DLL and log to the database. Within my logging DLL I perform additional work and hence it acts as a wrapper for log4net.
In my C# windows service project, in assemblyinfo.cs I added
Assembly: log4net.Config.XmlConfigurator(ConfigFile:="mywindowsservice.dll.config", Watch:=True)
This config file is where I set the appeneder. If I run my unit test it seems to work but it does not work in as a windows service. I am using system admin privileges so it is not an issue of previlige.
Any thoughts? |
|
|
|
|
Radovan Raszka
 |
| 10/03/2008 4:18 PM |
|
Hello,
problem is usually caused by wrong config file location. If you run windows service, then service's current directory is %systemroot%\system32, not directory, where service's EXE is located on the harddisk. Because your config file is referenced by relative path, it can be searched in wrong location.
I prefer configure log4net using app.config file or by special xml file, whose location can be got using service's EXE location (Assembly.GetExecutingAssembly().Location)
Radovan Raszka
|
|
|
|
|
Jeegnesh Sheth
 |
| 10/03/2008 4:21 PM |
|
Radovan,
Can you explain:
I prefer configure log4net using app.config file or by special xml file, whose location can be got using service's EXE location (Assembly.GetExecutingAssembly().Location)
In my test app, which had an APP.config, when I called my logging DLL, the right appender would get called I assumed by the attribute
[Assembly: log4net.Config.XmlConfigurator(ConfigFile:="mywindowsservice.dll.config", Watch:=True)]
If I use a special file, and I get the the Assembly executing path, how would I pass this information down to log4net dll, so that the right appender is called.
|
|
|
|
|
CLaco@SummitRacing.com
 |
| 10/03/2008 4:26 PM |
|
I run a service with log4net just fine using:
<Assembly: log4net.Config.XmlConfigurator(Watch:= True)>
The app.config nithe service project get's turned into myservice.dll.config that sites beside of myservice.dll.
For me, it just works.
-=Chris |
|
|
|
|
Jeegnesh Sheth
 |
| 10/03/2008 4:30 PM |
|
Chris,
My config file has the same name myservice.dll.config and I was hoping that the same attributes you have would work.
|
|
|
|
|
CLaco@SummitRacing.com
 |
| 10/03/2008 4:38 PM |
|
Had you tried enabling lob4net debugging?
Is log4net local to your service dll, or do you have it installed itno the GAC? |
|
|
|
|
Jeegnesh Sheth
 |
| 10/03/2008 4:39 PM |
|
It is local to the service and is placed in c:\program files\myservice\log4net.dll
Yes I enabled the internal debugging and added the section to write to file at c:\temp\log4nettest.txt and nothing is created
|
|
|
|
|
Radovan Raszka
 |
| 10/03/2008 4:41 PM |
|
there are 2 option:
1/ XmlConfigurator.Configure();
this configures log4net using app.config file, which must be in this form:
<? xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<appSettings>
....
</appSettings>
<log4net>
....
</log4net>
</ configuration>
This is probably best solution as you have only one config file for both application and log4net.
2/ configure log4net using extra XML file
XmlConfigurator.Configure(new System.IO.FileInfo(GetAppPath() + "log4net.xml"))
.....
public string GetAppPath()
{
string myPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
int i = myPath.LastIndexOf('\\');
return myPath.Remove(i + 1);
}
Both solutions works well with service, but when log4net config is changed, you must restart your service.
|
|
|
|
|
Jeegnesh Sheth
 |
| 10/03/2008 9:21 PM |
|
Radovan,
This is how my application is set up
LogUtil.dll
This has some custom logging as well as log4net dll reference and uses a custom appender to log
Myservice.exe has the following dll’s that it uses
- Logutil.dll
- Dosomething.dl
- Myservice.exe.app.config
Dosomthing.dll instantiates logutil.dll to write the logs.
I tried putting your xmlconfigurator.configure in logutil.dll and that did not work
I then tried placing it in dosomething.dll which did not work
Placing it in myservice.exe did not produce anything
Thoughts/ suggestions?
|
|
|
|
|
Radovan Raszka
 |
| 10/03/2008 9:57 PM |
|
Ok, there is also dependency on where log4net is set up.
Examples in my last mail works, if log4net is configured from Myservice.exe (not from DLL), and config is stored in Myservice.exe.config (you add app.config to the project, but Visual studio copies this file to the output folder as <projectname>.exe.config, what is correct)
If configuration is done from DLL, then logutil.DLL.config probably can not be used (at least it didn't work for me and I was told that application file is always searched as <processname>.exe.config), so save config into Myservice.exe.config or use second example.
Radovan
|
|
|
|
|
Jeegnesh Sheth
 |
| 10/03/2008 10:36 PM |
|
Radovan,
Myservice.exe has the following:
Default cstor
A main entry point into process, named main
Onstart and onstop methods
If I place the XMlConfiguartor.configure() in any of the methods above, my applications keeps crashing. Is there something I am missing? I am trying to use your first solution.
Many thanks |
|
|
|
|
Radovan Raszka
 |
| 10/04/2008 10:31 PM |
|
I configure log4net in main service class constructor. What kind of crash have you met - any exception (what?) ?
public class IPservice : ServiceBase
{ ...
public IPservice()
{
InitializeComponent();
this.CanPauseAndContinue = false;
this.CanShutdown = true;
this.CanHandleSessionChangeEvent = false;
this.AutoLog = false;
this.ServiceName = IPservice.SrvName;
XmlConfigurator.Configure();
log = LogManager.GetLogger(typeof(IPservice));
}
protected override void OnStart(string[] args){ ...}
protected override void OnStop() {...}
} |
|
|
|
|
Jeegnesh Sheth
 |
| 10/06/2008 2:22 PM |
|
Radovan,
LogUtil.dll
This has some custom logging as well as log4net dll reference and uses a custom appender to log. This is where I perform
Myservice.exe has the following dll’s that it uses
- Dosomething.dl -à uses Logutil.dll and is included as a reference.
- Myservice.exe.app.config
From your information, I assume I should do this in LogUtil.dll and in the default cstor of Logutil, I should obtain the path to the myservice.exe.app.config path and set it.
log = LogManager.GetLogger(typeof(LogUtil)); in the cstor
Myservice.exe does not need to do any logging, only DoSomething library needs to perform the logging.
The error I get is a generic .Net framework error showing all the assemblies which caused the error which may be due to versioning but the logs do not indicate this. So I did a clean install and that error was no longer the case but then it came down to just a system violation error each time I try to perform xmlconfigurator.configure() in the default cstor of the Myservice.
Thanks
|
|
|
|
|
Radovan Raszka
 |
| 10/07/2008 7:46 AM |
|
You can configure log4net anywhere - in main service EXE, in DLL which really does the job.... The only condition is to configure logging system before its first use. You can create Init() method somewhere in Logutil.dll to configure logging system and call it during application startup.
If there is problem to configure logging using app.config file, try to use the second way using separate config file to check if it works - if not, you have probably error in code related to config file location.
R |
|
|
|
|
harry.douglass@pnc.com
 |
| 10/09/2008 7:59 PM |
|
Jeegnesh,
You need to put add the following line in the AssemblyInfo.cs file of your windows service project:
[assembly: log4net.Config.DOMConfigurator(Watch = true)]
Your log should then appear in the installation folder containing that contains your windows service EXE based on what you put inside your App.config. This would be wherever you installed it on the server root drive (C:\) since a windows service is put inside the registry.
--Harry
|
|
|
|
|