Ejemplo n.º 1
0
 public void DeleteEventSource(string eventSourceName)
 {
     try
     {
         if (WindowsEventLog.SourceExists(eventSourceName))
         {
             WindowsEventLog.DeleteEventSource(eventSourceName);
             if (WindowsEventLog.SourceExists(eventSourceName) == false)
             {
                 _msg.Length = 0;
                 _msg.Append("Event Source ");
                 _msg.Append(eventSourceName);
                 _msg.Append(" delete succeeded.");
                 WriteMessageToLog(_msg.ToString());
             }
             else
             {
                 _msg.Length = 0;
                 _msg.Append("Event Source ");
                 _msg.Append(eventSourceName);
                 _msg.Append(" delete failed.");
                 WriteMessageToLog(_msg.ToString());
             }
         }
         else
         {
             _msg.Length = 0;
             _msg.Append("Event Source ");
             _msg.Append(eventSourceName);
             _msg.Append(" not found. No delete needed.");
             WriteMessageToLog(_msg.ToString());
         }
     }
     catch (System.Exception ex)
     {
         _msg.Length = 0;
         _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
         _msg.Append(Environment.NewLine);
         _msg.Append("Caller must have elevated security permissions (e.g. use Run As Administrator) to create and delete event sources and event logs.");
         WriteMessageToLog(_msg.ToString());
         AppMessages.DisplayErrorMessage(_msg.ToString(), true);
     }
     finally
     {
         ;
     }
 }
Ejemplo n.º 2
0
        public void OutputMessagesToWindowsApplicationEventLog(string eventSourceName,
                                                               int numInformationMessagesToWrite,
                                                               int numWarningMessagesToWrite,
                                                               int numErrorMessagesToWrite)
        {
            WindowsEventLog eventLog = null;


            try
            {
                if (WindowsEventLog.SourceExists(eventSourceName) == false)
                {
                    WindowsEventLog.CreateEventSource(eventSourceName);
                    if (WindowsEventLog.SourceExists(eventSourceName))
                    {
                        _msg.Length = 0;
                        _msg.Append("Event Source ");
                        _msg.Append(eventSourceName);
                        _msg.Append(" create succeeded.");
                        WriteMessageToLog(_msg.ToString());
                    }
                    else
                    {
                        _msg.Length = 0;
                        _msg.Append("Event Source ");
                        _msg.Append(eventSourceName);
                        _msg.Append(" create failed.");
                        WriteMessageToLog(_msg.ToString());
                    }
                }
                else
                {
                    _msg.Length = 0;
                    _msg.Append("Event Source ");
                    _msg.Append(eventSourceName);
                    _msg.Append(" exists.");
                    WriteMessageToLog(_msg.ToString());
                }

                eventLog = new WindowsEventLog(WindowsEventLog.EventLogName.Application, ".", eventSourceName);

                for (int i = 1; i <= numInformationMessagesToWrite; i++)
                {
                    _msg.Length = 0;
                    _msg.Append("Message ");
                    _msg.Append(i.ToString());
                    _msg.Append(" from test program.");
                    eventLog.WriteEntry(_msg.ToString(), WindowsEventLog.WindowsEventLogEntryType.Information);
                }

                for (int i = 1; i <= numWarningMessagesToWrite; i++)
                {
                    _msg.Length = 0;
                    _msg.Append("Warning message ");
                    _msg.Append(i.ToString());
                    _msg.Append(" from test program.");
                    eventLog.WriteEntry(_msg.ToString(), WindowsEventLog.WindowsEventLogEntryType.Warning);
                }

                for (int i = 1; i <= numErrorMessagesToWrite; i++)
                {
                    _msg.Length = 0;
                    _msg.Append("Error message ");
                    _msg.Append(i.ToString());
                    _msg.Append(" from test program.");
                    eventLog.WriteEntry(_msg.ToString(), WindowsEventLog.WindowsEventLogEntryType.Error);
                }

                _msg.Length = 0;
                _msg.Append("Number of event log messages written = ");
                _msg.Append((numInformationMessagesToWrite + numWarningMessagesToWrite + numErrorMessagesToWrite).ToString("#,##0"));
                WriteMessageToLog(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                _msg.Append(Environment.NewLine);
                _msg.Append("Caller must have elevated security permissions (e.g. use Run As Administrator) to create and delete event sources and event logs.");
                WriteMessageToLog(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), true);
            }
            finally
            {
                ;
            }
        }
Ejemplo n.º 3
0
        //NOTE: TestprogSystemObjects must have appManifest with line to request Administrator mode to run.
        //      DO NOT USE THIS APPROACH IN A SHIPPING APPLICATION.
        //      Try to reister the event source at application setup time.
        private static void RegisterEventLogSources()
        {
            StringBuilder msg = new StringBuilder();

            //check if the event source is registered
            //if not, register it and prompt user to restart the application
            //if it does, continue with the app start

            try
            {
                string logName                   = AppConfig.GetStringValueFromConfigFile("WindowsEventLog", "Application");
                string machineName               = AppConfig.GetStringValueFromConfigFile("WindowsEventLogMachineName", ".");
                string eventSourceName           = AppConfig.GetStringValueFromConfigFile("WindowsEventLogEventSource", "PFApps");
                string eventSourceInitializerApp = AppConfig.GetStringValueFromConfigFile("EventSourceInitializer", string.Empty);
                string currentWorkingDirectory   = AppConfig.GetStringValueFromConfigFile("EventSourceInitializerWorkingDirectory", string.Empty);

                if (currentWorkingDirectory.Length == 0)
                {
                    currentWorkingDirectory = Environment.CurrentDirectory;
                }


                if (WindowsEventLog.SourceExists(eventSourceName, machineName) == false)
                {
                    WindowsEventLog.EventSourceInitializer  = eventSourceInitializerApp;
                    WindowsEventLog.CurrentWorkingDirectory = currentWorkingDirectory;
                    int result = WindowsEventLog.RegisterEventSource(logName, machineName, eventSourceName);
                    msg.Length = 0;
                    if (result == 0)
                    {
                        msg.Append(eventSourceName);
                        msg.Append(" Windows Event Log event source name successfully registered.");
                        if (WindowsEventLog.OutputMessages.Length > 0)
                        {
                            msg.Append("\r\n");
                            msg.Append(WindowsEventLog.OutputMessages);
                        }
                        AppMessages.DisplayInfoMessage(msg.ToString());
                    }
                    else
                    {
                        msg.Append("Unable to register ");
                        msg.Append(eventSourceName);
                        msg.Append(" Windows Event Log event source name. Application event log write requests, if any, will be ignored.");
                        if (WindowsEventLog.OutputMessages.Length > 0)
                        {
                            msg.Append("\r\n");
                            msg.Append(WindowsEventLog.OutputMessages);
                        }
                        if (WindowsEventLog.ErrorMessages.Length > 0)
                        {
                            msg.Append("\r\n");
                            msg.Append(WindowsEventLog.ErrorMessages);
                        }
                        AppMessages.DisplayWarningMessage(msg.ToString());
                    }
                }
            }
            catch (System.Exception ex)
            {
                string errMsg = "Attempt to set event source name for windows log messages failed with following error: ";
                AppMessages.DisplayErrorMessage(errMsg + ex.Message);
            }
            finally
            {
                ;
            }
        }