enabling multiply sources writing entries to the same log using eventlog - c#

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.

Related

How often should I write to the Event Log in Windows?

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 !!! **

How to write a window service that track the number of times a specific folder was Opened

I Hope that this is the Correct way of asking this question. first my problem is that i want to know that how many times a specific folder was opened from the time my windows service start's. I don't want to write a desktop application for this purpose because i want it to happen in the background and also later i may want to add some more functionality. So that is why i need to be it a windows service.
is there some kind of OS Event that i can handle during my code, i.e the event is fired when a user open's folder.
If this is not the correct method then please let me know some other method that can help.
That's not possible in C#. You can be notified of changes within a directory and infer from that that the directory was opened--but there are many times when a directory is opened and nothing will be changed. What you're describing is most like a File System Filter Driver.
From What is a File System Filter Driver:
A file system filter driver can filter I/O operations for one or more file systems or file system volumes. Depending on the nature of the driver, filter can mean log, observe, modify, or even prevent.
Writing a filter is relatively easy, considering there are templates that you can use to base your work from. But, they do consist of kernel-mode code meaning they're not written with C# (they are typically written with C) and they are drivers.
for more details: http://msdn.microsoft.com/en-us/library/windows/hardware/ff540382(v=vs.85).aspx

Is there a way to monitor the state of a console application?

Is there a way to monitor the state of a console application?
I am trying to build a user interface which shows whether or not a console application is currently running on the server (processing files). If it is running, I would like to show the current state: how many files processed, what file currently being processed, etc.
The only way that I can think of doing this:
Create a text/xml file when application is started
Update text file with information about current state for each object it processes
Delete text file when the application is finished processing
To me, this doesn't seem like a very good or efficient way to do it. Is there a way to detect if the ClickOnce application is running, and perhaps some other way to access the "Messages" or Log of it to show the progress?
Note - I am also looking into using NodeJS to do this, but unsure if it has this capability.
First, you should consider writing this as a Windows service instead of a console application.
That said, scraping a log file that your application is writing is a reasonable approach. Just ensure that it never gets too big.
Alternatively, you could look at using custom performance counters. That would open the door to using System Monitor/perfmon as your monitoring tool, so no need to write any client code.
There are at least two ways to achieve that:
Your console application writes some logs, some state files, during its run, so other processes can read those files and understand what is going on in that console process.
Implement an IPC mechanism. There are different ways to do that. It may help you look in What is the easiest way to do inter process communication in C#?.

File deletion detect from any folder of my operating system and prevent the deletion by c# or win32 api

i know we can monitor a particular folder with FileSystemWatcher. with the help of FileSystemWatcher we can save the log which file was deleted. suppose i have windows service which will run all the time and if any user try to delete any file from my OS with specific extension then control then my windows service will show a messge box to user and prevent the user to delete that file. i just want to know can i do this with FileSystemWatcher class. if it is possible with FileSystemWatcher then please discuss here how or if not possible with FileSystemWatcher then how could i make it possible with my win service or normal win apps. would it be possible by win32 api?? . thanks
Use proper Windows security measures - File permissions together with access groups.
The only way is to have a filesystem filter driver which will track deletion and movement request (movement to recycle bin here) and will cancel such requests. You can write your own filter driver or use our CallbackFilter product. With CallbackFilter the task is trivial - less than a dozen of lines of code in user-mode (possibly in C#).

Polling directory on File Server

I need to write an application that polls a directory which contains images on a file server and display 4 at a time.
This application will be run up to 50 times across the network at the same time.
I'm trying to think of the best architecture to complete this requirement.
I was working on the idea of opening a file with read/write access and no file share allowed so that if another PC came in to read it it would error and it would have to move on to the next one, the problem is, is that I need to access all 4 images in sequence on the same pc ensuring other pc's dont try to open them. So for example if PC1 tries to open 1.jpg it needs to be able to open 1,2,3,4.jpg. If another PC comes in at the same time to read them I need a way for it to then open 5,6,7,8.jpg and so on and so on.
It seems a simple requirement but a nightmare to try and build successfully.
You're basically dealing with a race condition here, and I don't see a way to handle it from separate instances of your application running on separate machines unless you can guarantee your file naming will always follow a standard naming convention that would allow you to work with the sequence of 4 files using only the name of the first.
The best way to handle this would be using a centralized resource to manage access to your files, either a database as was suggested in a comment or else a service (such as WCF) that would "hand out" each set of 4 files.
What about creating a 1.jpg.lock file? The presence of a the file indicates the images are locked and any other instance of the application should skip that set.

Categories

Resources