Pass System/Environment Information to log4net - c#

It is possible to pass the system/environment information to the log4net log in my C# WinForms application?
It would be good to have details like what Windows version they are using, if any Service Packs are installed, what .Net they have installed etc.

I haven't used Log4Net for a long time, but can't you set this information to the global or thread context?
log4net.GlobalContext.Properties["WindowsVersion"] = windowsVersion;
Then you can output this information in your log file with the following pattern:
%property{WindowsVersion}
See http://logging.apache.org/log4net/release/manual/contexts.html for more information.

Related

the requested database is not defined in configuration while calling CreateDatabase method

I'm trying to use Enterprise Library to create database connection and it gives the error "the requested database is not defined in configuration"
I have 2 config files. One is for appSettings and the other is for connection string. We use a custom framework to take the config values from an external path in the system, eg.
C:\application\environments\dev\environments.config
C:\application\environments\dev\connections.config
The environment.config has a connectionString element which has the configSource="connections.config".
When I run the application, I'm able to get all the appSetting from the environments.config, but when I try to call the DatabaseFactory.CreateDatabase("dbConnection"), it throws the error. I'm not sure if I'm missing something or not. I read through many articles and couldn't find the exact problem.
I use Enterprise Library Common and Data dll and their version is 3.1.1.0. My .NET framework is 4.0.
Can anybody please provide me some idea to make this work?
Do I need to make any setting changes in machine.config?

How to do logging of C# Console application using EL 6.0

Can any one let me know how to implement logging with Enterprise Library 6.0 in C#. I want to do logging in Database, if it is available otherwise log the exceptions , information, messages into LOG file.
Can anyone tell me How to implement logging into Db, otherwise log in file dynamically.
I will have both logging DB and file config changes in App.config/Web.config.
So please help me on this how to implement logging dynamically based on runtime value:
If Db is available and accessible, then log, otherwise if DB is not accessible, then log to Log-file or event-viewer.
The new version 6 makes comprehensive use of the factory pattern, hence you need to set the logger up differently in version 6:
Try the following:
IConfigurationSource configsrc = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configsrc);
Logger.SetLogWriter(logWriterFactory.Create());
Logger.Write("logtest", "General");
Your description of your database logging requirements isn't quite clear, but I think these Code examples and links should be what you are looking for.

Rolling file for ETW EventSource .NET 4.5

I've been trying to work with ETW in .net 4.5.
I have a WCF Service and Console App, and I want which uses EventSource to write messages, however, I'm struggling to understand how to create my own ETW (EventSource and EventListener) for log to a file (rolling file).
Any suggestions?
In addition to magicandre1981's answer, you should add: -
TraceEventSession _session = new TraceEventSession(
"yourSessionName", #"C:\yourLogFile.etl")
{
CircularBufferMB = 100 //100mb rolling log file
};
_session.EnableProvider(TraceEventProviders.GetEventSourceGuidFromName(
"Samples-EventSourceDemos-EventLog"), TraceEventLevel.Always);
This can be in the same application as you are logging from (in process), or in a completely separate application (out of process).
Install the Nuget Package of Microsoft EventSource Library
Install-Package Microsoft.Diagnostics.Tracing.EventSource -Pre
and define the Events in a class which is derived from EventSource.
Now use the Semantic Logging Application Block from Enterprise Library to consume Events.
Here is a video how to use it:
Introducing Semantic Logging
http://channel9.msdn.com/posts/Introducing-Semantic-Logging

Writing log files

I am developing c# application, which is running as a windows service.
What ever transactions we are doing in the application i am writing it into log file.
A log directory is added in app.config file as below.
<add key ="LogDir" value="log" />
<add key ="LogLevel" value="2" />
And in the c# code the above one is accessing as below.
int logLevel = Convert.ToInt32(ConfigurationManager.AppSettings["logLevel"]);
if (logLevel > 0)
{
logger = new Logger();
logger.TraceLevel = logLevel - 1;
logger.logDir = ConfigurationManager.AppSettings["logDir"];
logger.logFileBaseName = "touchserver";
}
And then when any process is happening i am writing the data to the log as below.
TouchServer.Log(Logger.MessageType.Trace, 1, "Item successfully deleted");
And when i run my application in debug mode (i mean as console application) the log file will be created in the application's debug folder and the data will write into the log file.
But my problem is that when i install my application as service the log file is not getting created in the debug folder, and i am unable to see the actions performed , in case if anything went wrong.
Please help me to find a solution in this.
And i am installing service using Installutil command.
Thanks in advance
sangita
While you could get into why this is not working and fix the solution, overall there is no need to implement a logging component.
There are excellent free libraries available that do this very well. log4net is very popular. It is easy to use, feature rich and efficient. Take a look at it.
But my problem is that when i install my application as service the log file is not getting created in the debug folder, and i am unable to see the actions performed , in case if anything went wrong.
Check out what are the result of the IO operations by using Process Monitor. I suspect you'll find the identity being used to run the service process does not have write permissions where it is trying to write the log file.
But the better option is to use an existing logging library as Hemal suggests.

What is this "Log" class?

I see an application have used Log.info = "some info"
where are these logs created by the application ? where can i see that ?
you could use procmon to monitor which files are written to when you step over this line.
Check for potential configuration settings in App.config. If it is a 3rd party Logging Framework (e.g. log4net) there may be some clue in the App.config. Otherwise you'll need to post more info.

Categories

Resources