C# Windows Service Error Logging
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About c# windows service onerror Us Learn more about Stack Overflow the company Business Learn more about hiring
C# Windows Service Error Handling
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the c# windows service handle exceptions Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to write error logs
Error Logging In C# Windows Application
in Windows Service? up vote 0 down vote favorite I am new to Error Log. I am using this Code to write Error Logs. public static void ErrorLog(string Message, bool BlnActvty) { StreamWriter sw = null; try { string sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> "; string sPathName = Application.StartupPath; System.IO.Directory.CreateDirectory(sPathName); string sYear = DateTime.Now.Year.ToString(); string sMonth = DateTime.Now.Month.ToString(); string sDay c# windows service log to event viewer = DateTime.Now.Day.ToString(); string sErrorTime = sDay + "-" + sMonth + "-" + sYear; sw = new StreamWriter(sPathName + "ErrorLog_" + sErrorTime + ".txt", true); if (BlnActvty) { sw.WriteLine(sLogFormat + Message + " On Inserting"); } else { sw.WriteLine(sLogFormat + Message + " On Updating"); } sw.Flush(); } catch (Exception ex) { ErrorLog(ex.ToString() + " On Error Log ", BlnActvty); } finally { if (sw != null) { sw.Dispose(); sw.Close(); } } } By using the above Code i am writing Error Logs. But now, i want to write Error Logs in Windows Service. my Question is not in such a Range. I have Three Projects in a solution.One for Forms, Second for Classes and the other one is Windows Service. I want to write error logs in server where windows service is installed. Not in Systems where the Application is Installed. my application is running in 20 Systems in an Organisation. So in anywhere the Application get Error i want to write that error log in Server(Where the Service is Installed.). The Library is the One Which is Talking c# logging service error-handling remoting share|improve this question edited Nov 11 '13 at 5:22 Uwe Kei
here for a quick overview of the site Help Center Detailed answers to any questions you might
C# Windows Service Log File
have Meta Discuss the workings and policies of this site About
C# Windows Service Write To Event Log
Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads error logging in c# web application with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million http://stackoverflow.com/questions/19899324/how-to-write-error-logs-in-windows-service programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Logging Windows Service up vote 4 down vote favorite 3 I have the following code for logging my windows service which I found through research online. its done inside of my service class right before it is being initialized like http://stackoverflow.com/questions/15537047/logging-windows-service below. public GBBInvService() { InitializeComponent(); if (!System.Diagnostics.EventLog.SourceExists("MyLogSource")) System.Diagnostics.EventLog.CreateEventSource("MyLogSource", "MyDoLog"); eventLog1.Source = "MyLogSource"; eventLog1.Log = "MyDoLog"; } code for when logging: eventLog1.WriteEntry("GBBInvService Service Started"); I have however been advised against it by our consultant. His comments below: I’d avoid having the service create event sources at runtime. This requires the service to run with high privileges (i.e. more access to the computer than is really necessary). Setting up the Windows event log sources is really an install-time job. You may be able to add an event log installer to the project installer file (along with the service installer) and then the event source will always exist The problem with this advise is that I have not been able to find any examples where the logs are created in the project installer files. I also tried moving this log creation portion into my project installer but then it won't let me call or write to eventlog1 from my web service cs page. He has also sug
11, 20081 0 0 0 When you created a Windows Service, you usually add a Service Installer https://blogs.msdn.microsoft.com/helloworld/2008/12/11/how-to-create-custom-event-log-for-windows-service/ to allow this service to be installed using InstallUtil or installing it programmatically like this post shows you. The service installer will create the Event Log for the service, and by default it is "Application", and the source is the name of your service. The Event Source is most likely to be fine, but c# windows the event log may need to be set to something else. Unfortunately, the option is not visible from the installer. The Installer object for your windows service, actually hosted several other Installer objects, one of the child Installer is responsible to create the Event Log. You need to find this object and then changed the c# windows service Log property and it will create the appropriate event log. It is easy to find this object as this object is an instance of EventLogInstaller. The code: public partial class MyServiceInstaller : Installer { private const stringLogName = "My Service Log"; public MyServiceInstaller() { InitializeComponent(); EventLogInstaller EventLogInstall = null; foreach (Installer I in this.serviceInstaller.Installers) { EventLogInstall = I as EventLogInstaller; if(EventLogInstall != null) { EventLogInstall.Log = LogName; EventLogInstall.UninstallAction = UninstallAction.NoAction; break; } } } } That code above shows how to create a custom Event Log for your service, the code also shows that the custom event log will never be uninstalled, depending on the requirements it can be easily changed. If the event source needs to be changed as well, simply change the Source property and you are set. One thing to pay attention, if you create a custom event log, you install the service programmati