Logger Service need to have default config file for log4net / WISE - c#

I am developing a simple Logger Service (C#2.0) which logs message arrived (via MSMQ) from different clients. Logger Service logging mechanism is using log4net library. I want to have separate file for log4net configuration.
Service is supposed to be installed together with another applications in one common installation package (using WISE).
I want that Logger Service will be provided with that default log4net config file at first installation and write that file on installation directory only if it is missing, otherwise keep existing file.
Please help the newbie.
Have i manage it in installation package or in service.. or in both places?
What is the best way?
Any suggestions are welcome.

XmlCconfigurator.Configure(FileInfo ... )
Allow you to specify a file other than the application configuration file. For the WISE setup: check the docs, I'm sure there is some options to don't overwrite existing files.

Related

Template Log4Net Configuration

I'm trying to simplify our log4net configuration across a number of projects.
We have multiple services which all create logs. Their appenders are defined in each services app.config. This creates a lot of repetition.
I'd like to shift the log4net configuration out of here and into a Log4NetConfiguration.config file I can link to from each project similar to the accepted answer in this question:
Share log4net configuration across multiple projects
This way when developing we only need to change configuration in one place - but users can configure different services log files as they choose.
We call log4net.Config.XmlConfigurator.Configure() from a common utilities assembly - how do I get this to load the relevant version of Log4NetConfiguration.config from the running service?
As per StuartD's comment it's in the bin directory.
As such, you can just use Assembly.GetExecutingAssembly().Location to get the location of the config file relevant for the running application.
Then use XmlConfigurator.ConfigureAndWatch(fileInfo) as suggested.

Allowing a Windows Service to be configured

I have a Windows Service that I'm creating and I'm wondering what options are available in order for me let developers configure the service.
The service is part of an over all larger open source project and hence the service is going to be installed on lots of different machines.
Normal I would use a web/app.config for this but I'm not sure if this is possible.
Hence I am looking to so how others handle this case.
you do as you expect. You use the app.config, which will be renamed to <exeName>.configwhen the project is built and then <exeName>.config will be read by the service called <exeName>.
Settings are applied in a layered way and may come from other configuration files on the machine, such as machine.config. You can read about how configuration is handled on MSDN
EDIT
In response to comment: A service will only read the config when it starts (for perf reasons). If you want to reload the config file later, you need to handle that yourself I think.
You could read the last modified date/time of the config file to determine if the file has been changed, or setup a file system watcher and then tell the configuration manager to reload that section again next time it is read, by calling ConfigurationManager.RefreshSection("appSettings") and that section will be reloaded from disk when you next access it. See the ConfigurationManager MSDN docs
You can just use a .config file with the same name as the exe that is the service.
If your service runs as MyService.exe, it's config file would be MyService.exe.config.
In Visual Studio, just add an Application Configuration file. This will add an app.config file to the project.
You can then access things like AppSettings and ConnectionStrings using the ConfigurationManager class, just like you do with ASP.Net applications.

Can multiple C# apps use one App.Config file?

We have many C# console apps that run on scheduled tasks. All of these apps have their own config file, which contain settings like our smtp server. If our smtp server ever changed, we would have to manually go into each config file and change it. Can multiple apps look at 1 config file on the C: drive, or is that considered bad practice? Using the database to store values is a no no.
You can point to external config files inside your application's configuration file like the following, and have all your applications use the same set of settings from a single file:
<appSettings file="c:\CommonSettings.config">
<add key="MyKey" value="12"/>
</appSettings>
For more information, you can read following articles:
AppSettings can Reference an External Config File
How to share custom application configuration settings across projects in .NET
It is not directly possible to share one application configuration file because the .config filename needs to match the executable name (so for example.exe it would be example.exe.config).
It makes sense to have separate values for the different applications, as they are separate applications.
If there are configuration sections that you do want to share, you can use the configSource attribute to point to a file. The appSettings section also has a specific file attribute that you can use in the same manner.
If there are certain configuration values that are shared across all applications, you can consider placing them in the machine.config file for the version of the framework you are using.
Can you use custom xml files to store configuration data ?
There's no necessity to use app.config.
Using Cinchoo framework you can achieve this, by simply creating custom configuration object and use it all the console applications. All of them will read from same configuration file. For more information, please visit http://www.cinchoo.com

Question regarding App.Config for class library and logging

I have a class library for which i want to have configuration file.
The purpose of the configuration file is to have the path and other parameters.
Also i wanted to use the Enterprise logging block in Class library.
Here is my scenario:
This is a Class library and will be deployed in GAC
Enterprise logging blocks uses app.Config.
My calling application which consumes the dll is BizTalk 2010.
I don't have permission to modify the BizTalk's application config file
What i am trying to achieve is:
My Dll needs to use a configuration file which has many configuration parameters
Also as i understand, I need app.config for Enterprise Logging
How can i achieve this?
Any ideas?
Cheers,
Karthik
The easiest way is to use the machine config. You also have the option to put your configuration in a separate file and then reference it from the machine config.
You can also set the values in code but that sorta defeats the purpose of using a config file to begin with.
Modify your class library to open and read config file from specific location.
Do you have access to the calling application's code?
If so, maybe you can explicitly load the config file as suggested by Marc_s's solution?
Just an idea.

Using Log4Net during setup application

I'm having trouble seeing how I can use logging in my setup / install project.
I've got Log4net working on installed applications, but I can't seem to log the install / uninstall process.
My main problem is with the logging config file.
I suppose it's a bit of a chicken/egg scenario - theres no way for me to grab the just-installed logging file?
I have a method that finds the root directory of my app, using - AppDomain.CurrentDomain.SetupInformation.ApplicationBase
and i normally use this to locate the logging config file.
This directory however, during install, is not where it is installing (obviously) it is somewhere within the windows filesystem.
Does anybody have any ideas to this?
Consider configuring log4net in code and not using a file or storing the configuration as a file resource of your custom action assembly and using XmlConfigurator.Configure(Stream configStream) overload.
Of course if you want to reuse the configuration of the application this is not a clean solution.
Also checkout this question:
log4net pure code configuration with filter in c#
When executing MSI's you are bound to msiexec's logging mechanisms (which is not as friendly as log4net).

Categories

Resources