I am using Log4net to produce different kind of logs and using RollingFileAppenders which rolls on a given size.
Now i have a new requirement that a log should be visible on a windows form therefore i am looking for some event that can be handled on each log entry within my application so i can display that particular log entry in my form as well.
Or there may be some appender which gives such functionality.
Any idea on this?
I'd suggest writing your own appender - it's really easy to do:
Create a class that inherits from AppenderSkeleton
Override the Append method
Simple!
Here is an example of a custom appender that updates a textbox.
As I suggest in this answer, you could set up a MemoryAppender and monitor log messages in memory. It does not expose an event but you could easily set up a timer that retrieves the latest messages.
Related
I have an automated Process that will run a certain task every hour. I am thinking of creating a logging for this that would allow the user to see, if they want, what values are being used. Should I do this in the Event Log or create a .log txt file for this?
Is there a guideline for Windows Event Logs and whether it should only be used for errors only?
Are there any issues that I should be on the lookout for if I write to the event log every hour?
First of all one line answer to question
How often should I write to the Event Log in Windows
It depends on your needs and frequency at which you want information. There is no "one size fits all" in case logging decisions.
Should I do this in the Event Log or create a .log txt file for this?
depends on your requirements and who is going to use the log. To consider the Event Log, are you sure your application will have access to write event log every time? (in simple words, Administrative privileges).
If there are no set of standards defined (assuming you are not writing it for personal use only), then you should set a convention/standard in place for organization for what to write in Event Log and what to write in Log file.
For example
Event log: Unhanded exceptions and warnings
Log file: Caught exception and General Information (so that if in any deployed app client can send you the log file on mail)
Is there a guideline Windows Event Logs and whether it should only be used for errors only
You can visit Enterprise logging library:
https://msdn.microsoft.com/en-us/library/dn169621.aspx
Edit:
**Why downvote without a reason !!! **
I have some external libraries that I am using that are logging to the console. I want these to log via log4net.
NOTE: I am NOT wanting to log to console from log4net, that should be straight forward.
What I have discovered thus far:
1) Console.setOut method allows using a different file stream.
1.1) Overriding memorystream seemed promising but there isn't a chance for raising an event to notify of changes
2) Writing to a file from Console seems like a work around, where I can read the file to update the UI textbox with new logs
3) FileStreams can autoflush, this means automatic updating of information. This sort of concept is similar to what I am after?
Whats the best way to get the console information put into log4net so that it can publish console log items the same way as log4net is configured? Currently my log4net puts logs into the eventlog, into a databinded wpf textbox, and into a file.
Personally don't know any other solution for this case other then you wrote:
ovewrite output of console pointing it to a file
read the file and add to a logger
To be notified about the change you can try to use FileSystemWatcher http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx.
Or if you don't want "real time notification", canopen file only for read and check with the timer if there is any row after last saved reader pointer position.
But I think, the first option is much easier.
Hope this helps.
My program moves files from one folder to another immediately upon creation. The files will be coming in by about 50 per second. The program monitors two folders and sends files in those two folders to certain directories on the computer.
The one thing I want to get sorted out is the errors that could potentially pop up. I thought about using messagebox.show to let the user know lets say "the folder does not exist," but with 10000 or so files unable to be moved, or if access to directories is denied, the computer might just run out of memory with 10000 popups.
An alternative is simply to display a message to the user on the GUI control, and stop the file watcher. The problem is, my folderwatcher class cannot access the status bar in my GUI. So if the status bar was called messages, I cannot simply assign "messages.text" to the error message from the exception thrown in the folderwatcher class.
I was also told that it is bad practice to give the control on the GUI to the folderwatcher class as it defeats the purpose of object oriented programming. (something along those lines).
So, I thought about constructing a class that simply stores data/status messages. My GUI will hook in to that class and the folderwatcher class will update it when an error occurs. When that happens, my GUI will update and the messages.text will show the error for the user to see.
I think this concept will work quite well, but I'm not sure where to start. If you need any additional information I'll be happy to provide, but feedback and hints/tips are what I'm looking for.
Also, is my method of constructing a separate class holding information the best method to transferr error messages between the class and GUI? or is there an easier way to do it?
Sincerely,
tf.rz. Thanks for your help! It is greatly appreciated.
Build a private method with strings with different values and when an Error Pops, Just Get and Set the Value to the Appropriate Messagebox. Which could be assigned as a global variable.
I've hooked the class and the GUI via another class which contains just one string and an event handler. The GUI subscribes to the event handler and the class will create an instance of the new class created and modify the string. When the string changes, the GUI will be updated and the user will then see the message. Thank you to all who contributed!
I have written two different applications running as windows services. Each application represents one source to the windows logs.
1.) Question:
I want the applications to write entries to the same log e.g. my_applications_log. My motivation is to monitor order of the actions taken by the applications. Problem is that when I try to write an entry with one source and then another entry with another source to the same log, only the entry with the latest source registered is written to the log, the other one not. I don't understand why this should not be possible.. I know one way is to split logs according to sources and use custom view to have them "joined". But I would like to have one specified log not two or more..
2.) question:
In Windows Event Viewer I can see that logs can be organized into "directories" e.g.: Application and Services Logs->Microsoft->Windows->Audio->{CaptureMonitor log, Operational log} . I can not find any API allowing to create such a directory and the some logs within this directory. Is this possible somehow??
thanks in advance
If you're trying to share a log file across processes, you need to protect that file with a lock of some kind, or have the applications retry if a write fails. It's not possible for two processes to be writing to the same file at the same time. Two processes can have the file open for writing, but if they both try to write at the same time, then you'll likely get an exception.
Probably the easiest way to do this is with a Mutex. Each application would create a named Mutex at startup, using the same name:
Mutex LogLock = new Mutex(false, "LogLock");
Then, when you want to write to the file:
LogLock.WaitOne();
try
{
// write to the file
}
finally
{
LogLock.ReleaseMutex();
}
If you want to write to the Windows event log, look into the System.Diagnostics.EventLog class. I don't have any experience with writing to the Windows event log, so I can't say whether that will work for you.
I have an application that has created a number of custom event log sources to help filter its output. How can I delete the custom sources from the machine WITHOUT writing any code as running a quick program using System.Diagnostics.EventLog.Delete is not possible.
I've tried using RegEdit to remove the custom sources from [HKEY_LOCAL_MACHINE\SYSTEM\ControlSetXXX\Services\Eventlog] however the application acts as if the logs still exist behind the scenes.
What else am I missing?
I also think you're in the right place... it's stored in the registry, under the name of the event log. I have a custom event log, under which are multiple event sources.
HKLM\System\CurrentControlSet\Services\Eventlog\LOGNAME\LOGSOURCE1
HKLM\System\CurrentControlSet\Services\Eventlog\LOGNAME\LOGSOURCE2
Those sources have an EventMessageFile key, which is REG_EXPAND_SZ and points to:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\EventLogMessages.dll
I think if you delete the Key that is the log source, LOGSOURCE1 in my example, that should be all that's needed.
For what it's worth, I tried it through .NET and that's what it did. However, it does look like each custom event log also has a source of the same name. If you have a custom log, that could affect your ability to clear it. You'd have to delete the log outright, perhaps. Further, if your app has an installer, I can see that the application name also may be registered as a source in the application event log. One more place to clear.
What about using Powershell?
Remove-EventLog -LogName "Custom log name"
Remove-EventLog -Source "Custom source name"
I was able only to delete it by using:
[System.Diagnostics.EventLog]::Delete("WrongNamedEventLog");
in powershell
Perhaps your application is fault-tolerant, meaning that it checks to see if the event log source is already registered and registers the source if it isn't?
If this were the case, your application would re-create the source(s) each time it ran, no matter what you did.