Error message when writing to the Windows Event Log - c#

When I run a piece of code that I wrote I keep getting the following error:
The source was not found, but some or
all event logs could not be searched.
Inaccessible logs: Security.
I have double checked and I am not manually writing to the logs anywhere, so I assume my application is doing it on it's own.
I found this post, which suggest that I give read/write permissions to a registry entry that corresponds to the Event Logs. I had three questions about this:
Is this the correct way to fix this
error?
If I go this route, should I give permissions to whatever my app pool is running under? (it's a web app)
Is it secure to give read/write access to that registry key?

This definitely seems like a permissions issue. It has to do with the fact that the Source you are trying to write into the event log does not already exist. Once it is created, this error should go away. Since you just need to make sure the Source gets added that first time, you could try the registry approach (grant access to the AppPool user), and then remove the registry access after the source has been created.
I've worked around this issue by manually adding the Source to the Event Log using a dialog app I wrote that I run as Administrator. Once the Source is added, all of the other event logging works fine. If you know the Source that it's trying to use when it writes to the event log, you could try that approach as well.

Related

Log Every File Access Attempt on a System and Sort it by User/System Account

I have a strange issue where I need to see why my application pool is crashing due to a permissions issue and no logs are helpful. Is there any way to log every file access attempt on a system and sort it by the user/system account that tried to access it, especially if it fails in accessing the desired file? Even if there are third party applications for this, it would be very helpful.
Process Monitor by Sysinternals/Microsoft will show that and a lot of other activities.

Console Application stopped working

Is it possible for code to work perfectly on the Administrator profile and not work at all while logged in as another user?
I am building certain applications at work, and while developing, coded and tested while logged on as Administrator. Every application working normally.
Now before deployment, when I execute the same application, but now logged in with a specific user account, I just get the error "Console Application stopped working" etc etc. I have handled all possible exceptions with my own Message Box, but this error is something I did not expect?!
In the exe's, I tried changing setting to "Run as Administrator", no use!
Any help would be much appreciated. Thanks!
PS - There are certain FTP methods I reuse from another class. And by handling all possible exceptions, I mean, I would have a try-catch block for the FTP method, so in case of error encountered after deployment, affected user may call helpdesk and notify the exact error.
Well, its not the right way to go, but right click and run as Admin, works butter smooth. Should I go ahead and modify all executables to - Run as Administrator under Properties->Compatibility??
You should firstly investigate if your application does need Admin rights indeed. A few scenarios that I can think of where an App needs admin rights are writing to protected locations of the system, writing to registry (excluding the areas that the user has rights on). If you think that admin access is not needed, you could always seek other ways to make it work. For example, instead of writing to a protected location, create a directory for you application in the user's application data folder and write to that location.
When you launch the application, if you're running on I believe Windows Vista or higher(7 works this way for sure)
You can Right click on the application and click on Run as Administrator
And yes, Sometimes the application won't work if you're not running as an administrator. For instance, If it's trying to change files in a protected location.
There are other variances that can cause your program to not work, For me, "Console Application stopped working" normally means that you don't have the right version of the .net framework installed, but it can mean any number of things.

Determine whether the SQLSERVR process has write access to a given folder (C#)

I've got an winforms application which uses a SQL Express back-end (which is always on the local machine).
I've run into a variety of user issues where a user performs a task that creates a new database in a particular location, but it turns out that the "NT Authority\Network Service" does not have sufficient permissions over the location they specify.
I'd like to check at runtime if the user running SQLSERVR has access to a particular folder.
I've got WMI code running to determine the user/SID of the SQLSERVR process. My next step is to try to get the WindowsIdentity/WindowsPrincipal of the user so that I can query the file access rules.
But I'm stuck - how can I reliably get a WindowsIdentity when all I have is the user/domain/SID? It doesn't seem like there's a constructor that is well suited to this?
You would have to pinvoke LogonUser to obtain a token. I seriously doubt that's going to work, these service accounts are highly privileged.
Do ask yourself if this is worth the hassle. Maybe a warning is desirable but there is nothing that you could do yourself to fix the problem. It is going to require an administrator to really fix the issue. Focus on getting a good diagnostic out of the exception you get.
Can I suggest you take a different (simpler) approach? Try just impersonating the service/user account and write a quick temp file and then delete it. If you can write that file (and delete it) you can be sure that the SQL Server can do the same thing.
Here's a post that talks about impersonating the NETWORK SERVICE account: http://geek.hubkey.com/2008/02/impersonating-built-in-service-account.html

Strange Problem with a .NET Windows Service

I have two Windows services written in C# following the same patterns and methodology.
Both services were development tested against a Windows 7 VM and QA tested on Windows Server 2008 VM. Both services have been installed and uninstalled many times under these test environments without issue, however upon installing in the production environment (Windows Server 2008) one of the two services refuses to start.
To install the services we are using InstallUtil.exe with ServiceInstaller and ServiceProcessInstaller components attached to the service.
By all appearances, the failing service installs successfully. InstallUtil.exe reports success and the service appears in the Services snapin. You can also locate the service in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Blah Blah. However, if you attempt to start the service you get the following:
net start blah.blah.exe
"The service name is invalid."
...or going through the Services snapin...
"Windows could not start the "Blah Blah" service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion."
I have added some event logging to the constructor of the service class failing service, but it does not appear to get called.
As this is a production box, there is no Visual Studio on the box and remote debugging is out of the question.
Is there any other way for me to gain debugging info on why the failing service isn't starting?
Is there any other obvious-ish reason that I might see this kind of issue?
Edit: I should have also mentioned.. The only other evidence of a problem in the Windows Event Viewer is two messages in the System log from the Service Control Manager:
"A timeout was reached (30000 milliseconds) while waiting for the Blah Blah service to connect.
"The Blah Blah service failed to start due to the following error:
The service did not respond to the start or control request in a timely fashion."
Edit: Resolved
The issue ended up being a combination of a configuration mistake and a bug that was hiding it. See my answer below for more details.
Jeopardy Answer: "How might invalid custom configuration combined with a bad global exception handler manifest itself in a .NET Windows service?"
Figured it out.
The root cause of the problem was an invalid custom configuration section in the app.config. We use a custom configuration section to configure the service from the app.config and the assembly and namespace of the ConfigurationSection derived class had changed recently.
As it turns out, our production configuration was looking for the definition of the custom ConfigurationSection in the wrong assembly and the exception thrown when failing to instantiate it was getting hidden by a bug where exceptions caught early in the life of the service would attempt to be logged to the custom log rather than the Application event log. (Since the event log source did not exist on the custom event log, this would throw another exception from the global exception handler and the service would die in the constructor.)
This second exception did not get logged anywhere and we only found it through code inspection.
The resolution was to fix the configuration and to modify the global exception handler to only attempt to write to the Application event log using the service name as the event log source. (InstallUtil registers the service name as an event log source on the Application log.)
Thanks for the help everyone! Sorry this particular issue ended up being so specific to our setup.
It could be possible from the error message that you've described
net start blah.blah.exe "The service name is invalid."
That the name that you gave the service in the service install component that you added in visual studio is not what you think it is.
I've had this problem quite a few times with developers misnaming services in installs.
what are you trying to do on service start?
also check the account through which the service is running and the account has the necessary privileges.
I've run into problems like this many times while programming my own services, so I'll just list out a bunch of things that at various points solved my problems and hope that they help you:
I've had to restart services.msc because I've uninstalled a service it still thought it had a reference to. However, when I would start it, the "service was invalid"
If you're making the service from a console application (so that it can be debugged) but forget to change it back to a service, it won't start.
When I was using InstallUtil.exe, sometimes it would attempt to install multiple copies, so I switched to just using a Setup Project.
Hope that helps in some way.
This is a common issue. What code do you have in your Start event?
You should only have code that activates a Timer when the service starts. This allows the Start event to complete quickly and notify the controller. If the execution takes longer than that, you will get the error you received. There might be some reason (possibly data-related) why its taking longer in production.
When the Timer ticks, execute your code and stop the Timer. Obviously, also put everything in try/catch and log the exceptions.
It is possible for this error to occur when
your service is depending on another service/application
or due to invalid app.config configurations
Encountered the same issue when Entity Framework was unable to connect to the database server due to lack of permission.
Better to add a global level error logging mechanism to your application to debug the issue easily.Event Viewer might not reveal exact details in some scenarios.

How do i read the eventlog of windows service

I am trying to modify an already written Windows service.
I see the source has an System.diagnostic.EventLog object which logs different action of the service.
I am trying to figure out how do i read this log. From semantics it seems that Eventlog is logging several actions of service.
where is this log stored, how can i see it, i need access to it as my service sometimes stop intermittently.
I tried to google it but did not find any solid answers. Putting the questions simply what is an eventlong and how do i use it.
In 'Computer Management' you'll see an entry labelled 'Event Viewer'. Depending on which version of Windows you're running, there will be several sub-entries below this conforming to each of the event logs Windows maintains. Your application will probably be logging to the 'Application' log, although it might also log to 'System'. Click these subitems to have their log contents show up in the central pane.

Categories