Пример #1
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
            {
                ;
            }
        }