Log4Net won't create custom event log in event viewer - c#

My WPF app uses Log4Net to record messages to the event viewer. This is working great on most machines. However, there are two machines in my office where there are problems. One is a physical Windows 7 machine with 2 GB of ram, the other is a virtual machine running XP, which also has 2 GB of ram.
The problem is that even though the users are logged in using accounts with administrator rights, the system won't let them create the custom event log that I set up for my application. This is causing my program to die.
I can add error handling on all of the Log calls, but my feeling on this is I shouldn't. The messages are being logged in the catch handler for another error that already occurred. Just what am I going to do with the error information if it can't be logged?
In any event, I tried to create the custom event log on the XP virtual machine yesterday and it still wasn't created. What exactly do I need to do to get the custom event log created on these two machines?
Tony

It turns out that the problem wasn't in the logging code at all. My program uses WPF for the GUI. It's start-up sequence does the minimum amount of work on the UI Thread so it can display the UI as soon as possible.
The rest of the initialization is done on a background thread. I knew that an error was occurring, but I couldn't find the custom error log in the list of logs in the Event Viewer. It turns out that my code didn't find some data in the database that it needs and was trying to report the error. This is a 2 step process which involves first recording the error to the log and then displaying a custom MessageBox dialog. I was getting a XamlParseException when the program was trying to display this dialog.
To make a long story short, the problem that was crashing the program was the XamlParseException. This was thrown because I was calling the custom MessageBox's Show method on the background thread, not on the UI thread. Because I couldn't find the custom event source in the event viewer, I couldn't find the error, so I assumed that the error was a permisions issue.
By the way, I did try to create the event log manually at one point, and yesterday I checked the registry and did find the entry for the custom event source.
There is one other machine here that is having the same problem. I'm sure it's the same exact issue. I'm adding logic to the error handling to make sure that the custom MessageBox is always called on the UI Thread so the program won't bomb like that if the same issue recurs.

We would need to see how you tried creating the event log on the XP machine...
Generally, you need to read this: http://msdn.microsoft.com/en-us/library/49dwckkz(v=vs.80).aspx
Particularly the note discussing when to create your custom event log:
"In general, create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources and you attempt to write an event with the new source, the write operation will fail. If creating the source during installation is not an option, then try to create the source well ahead of the first write operation, perhaps during your application initialization. If you choose this approach, be sure your initialization code is running with administrator rights on the computer. These rights are required for creating new event sources."
Try creating the custom log in advance of the first logging event to use it.

Related

How to launch my C# application when any application starts in Windows 8?

I want to launch my application, like the windows security prompt, before any application is launched in Windows 8.
Is there any event handler, which gets notified whenever any application is launched?
My use Case : I want an App similar to a child lock(Lets call it myCustomApp).
When any user runs a game(say Solitare), i want myCustomApp to check the process name, and kill the process immediately.
P.S. : i am quite new to programming.
Thanks in advance!
Is there any event handler, which gets notified whenever any application is launched?
Yes: you get use WMI events to detect new instances of Win32_Process.
But these are created with process creation, not before.
Doing something between the call to ProcessCreate that creates the new process, and the process actually being created is going to be, at best hard (you might need to do it in the kernel), but quite possibly impossible.
Why do you want to do this? What problem are you trying to solve? This really does sound like an X-Y problem.
Edit:
The term you need to use is hook: the interception of some operation on windows. Eg. "Is it possible to hook the creation of windows globally so I can control where the windows are placed on the screen?"
There is a direct way in the kernel: PsSetCreateProcessNotifyRoutine
There are helpers in user mode (eg. EasyHook), but these require injected your code into each process (which anti-malware tools are likely to object to).
But you should still start out by looking for better approaches to you underlying problem.

Program developed by .net 4.0 sometimes cannot start after reboot

I have a program developed by C# and build by .net 4.0.
This program is a windows client which would read the barcode from a barcode reader (via com port) then send the barcode to back-end server via WCF.
The customer have a script in the client OS which would reboot the OS and automatically start up my program every day. The OS is Windows XP Embedded.
Now the problem is, sometimes when the system reboot, my program cannot be started and an error message box will popup to ask whether send this error report to Microsoft.
The most strange thing is, if my colleague copy the program folder and paste as "Copy of ...." in the same folder with the original one the exe under "Copy of ..." one can run without any problem. But the original one still cannot.
What my speculation is maybe the program was writing log and other record files while the system was forced to reboot. And the files get the read/write lock unreleased.
I have uploaded the error screen shots to flickr. Please click here link to visit.
Without knowing what the actual exception is, we can only guess.
You will need to catch the exception that is being thrown in your application.
The best practice is to encapsulate your code in try/catch clauses.
If you are still getting application crashes, then you can attach an event handler to AppDomain.UnhandledException, or Application.UnhandledException and log the exception(s) being received.
Make sure to output the entire exception stack trace, so you can see where it's being thrown from.
Once you've got the exception, if you can't figure out the cause, then ask another question here with that specific detail. eg: "I'm getting an FooException being thrown when I call Bar() after start-up on Windows XP Embedded"
Sometimes after a reboot, some device drivers, or some hardware, will NOT reset itself. The machine has to be power cycled (turned off and back on) or a command needs to be discovered that will force the device driver and/or hardware to reset.
Referring to image IMG_1348 you posted, the error is thrown in your form constructor.
Seems like either code you added or InitializeComponent code is throwing.
Since you are using XPe, you have some options to debug this issue:
Add message box statements around the various constructors to show initialization progress. Guard before and after.
public Form1()
{
MessageBox.Show("Before InitializeComponent");
InitializeComponent();
MessageBox.Show("After InitializeComponent");
//MessageBox.Show("Before Other");
//Other Initialization Code
//MessageBox.Show("After Other");
}
Attempt to use the remote debugger. I am not sure if this works on XPe, but if it does, and since your code is throwing in the constructor, you need to add code to wait until the debugger is connected.
public Form1()
{
while (!System.Diagnostics.Debugger.IsAttached){ System.Threading.Thread.Sleep(0); }
InitializeComponent();
//Other Initialization Code
}

DevExpress XtraSpellChecker randomly stops working, shows all words as mis-spelled

We recently released a desktop record keeping product that required a simple spell checker on a couple text box fields. We use DevExpress 10.1 XtraSpellChecker. It does exactly what we need but customers report that occasionally it simply stops working and shows all words as being mis-spelled.
My guess is that it cannot open the dictionary but I have no clue as to why. Typically stopping and restarting the program solves the problem.
Most instances of this error occur on computers with single users who have full modify rights on the installation folder ( location of dictionary ).
Anyone experience the same thing, have ideas of what might be causing this? Search of DevExpress forum has proven fruitless. It does happen on more than one installation but several have reported no issues.
Oh, and we are using it in Spell As You Type mode.
The check as you type mode works by checking the text in a background thread. Such a situation can occur if an exception is raised in the background thread that terminates it. Try to include a handler to all exceptions raised in your app and ask your users to send you the log when this problem occurs. To learn how to catch exceptions in all threads, please refer to the code snippet posted in the
Application.ThreadException Event MSDN article.

Create separate crash handler like GoogleCrashHandler - C#

How can I create a separate crash handler like GoogleCrashHandler?
This is appropriate for unmanaged code (all of Google's code is). The usual scenario is that an app opens a named pipe to talk to the crash handler and tells it to start watching the app. The crash handler then adds a named event to a list it watches. When the app crashes, an exception filter inside the app, installed with SetUnhandledExceptionFilter() will gain control. That exception filter then turns on the named event. The crash handler immediately notices and it takes a minidump of the crashed app and uploads it. And terminates the app.
A crash handler like this is necessary because a crashed app cannot be trusted to still be able to function correctly when it suffered a heart attack. Microsoft has one too, it is built in Windows. Called WER, Windows Error Reporting. That's the source of the dialog you see when a crashed app asks you if it is okay to let Microsoft know about the crash.
This is approach is unnecessary for managed apps. They almost never die from the kind of hardware exceptions (like AccessViolation) that unmanaged apps die from. Just write code for the AppDomain.UnhandledException event.
The Google Crash Handler seems to be an application that gets notified when certain things occur in other applications.
"GoogleCrashHandler.exe runs continuously on your computer if you've selected to send anonymous usage statistics and crash reports to Google for certain Google software, like Google Chrome. It helps send crash details to Google when your Google software unexpectedly shuts down. We use this data to help determine how to prevent these errors from happening in the future."
I would create a Windows service, that runs in the background, that you communicate with through named pipes, tcp, file drop, or any other good method.
The service would then send a notification to a specific server ( or list of servers),
with the information it collects. It can act as a buffer, so you dont have to send a notification to the server every time you get notified internally.
I would let the service call a web service on a server, when its ready to spill its beans.
Every application you create after that should be able to check if your "crash handler" is running and notify it whenever appropriate about errors or exceptions etc.
However since this behavior will probably trip some local firewall programs, and users might question what data is being sent and when, you will want to document this well, and be very upfront with what data you are collecting, why you are collecting it, and where it is being sent.

C# trigger, Surround SCM & werfault.exe

We are in the process of setting up surround scm as our source control program. We created a trigger which will run when changing the state of a file/repository. When we run it on many files the server gets several werfault.exe processes in the process list. I realize its windows error reporting, however, there is no popup. I'm trying to determine the cause of the error... is there a specific log I can check, or a debugging technique I can use? I don't believe it will be possible to debug directly on the server it runs on.
Thanks
WER faults end up in the Event Log, so that might give you a tiny clue, though it's usually not enough info to tell what went wrong. Maybe you want to add more logging to your trigger application, or have it run userdump.exe when it's about to crash (see AppDomain.UnhandledException event) to a specific folder - that way you can open up the crash dumps later to figure out what went pear-shaped.
What was happening was that we set a trigger on certain events and the trigger calls the handling exe simultaneously, which was swamping the server. We're now using an alternate solution with a windows service so we can control the max amount of simultaneous trigger executions.

Categories