Example #1
0
        public static void WriteEvent(string sourceName, string eventMessage, int eventType, int eventID)
        {
            if (!System.Diagnostics.EventLog.SourceExists(sourceName))
            {
                EventSourceCreationData SourceData = new EventSourceCreationData("", "");
                SourceData.LogName     = _logName;
                SourceData.MachineName = ".";
                SourceData.Source      = sourceName;
                System.Diagnostics.EventLog.CreateEventSource(SourceData);

                EventLogPermission eventLogPerm = new EventLogPermission(EventLogPermissionAccess.Administer, ".");
                eventLogPerm.PermitOnly();
            }
            System.Diagnostics.EventLogEntryType type = (System.Diagnostics.EventLogEntryType)eventType;

            while (true)
            {
                int logMaxLength = 31000;   // Log entry string written to the event log cannot exceed 32766 characters

                if (eventMessage.Length <= logMaxLength)
                {
                    EventLog.WriteEntry(sourceName, eventMessage, type, eventID);
                    break;
                }
                else
                {
                    EventLog.WriteEntry(sourceName, eventMessage.Substring(0, logMaxLength), type, eventID);
                    eventMessage = eventMessage.Substring(logMaxLength, eventMessage.Length - logMaxLength);
                }
            }
        }
Example #2
0
        public static void WriteEvent(string message, EventLogEntryType eventLogEntryType = EventLogEntryType.Error, int id = 2030)
        {
            string eventViewerLogSource = Durados.Database.LongProductName;
            string eventViewerLog       = "Application";

            if (!(EventLog.SourceExists(eventViewerLogSource, ".")))
            {
                EventSourceCreationData eventSourceCreationData = new EventSourceCreationData(eventViewerLogSource, eventViewerLog);
                EventLog.CreateEventSource(eventSourceCreationData);
            }

            EventLogPermission eventLogPerm = new EventLogPermission(EventLogPermissionAccess.Administer, ".");

            eventLogPerm.PermitOnly();


            EventLog evLog = new EventLog();

            evLog.Source = eventViewerLogSource;
            evLog.WriteEntry(message, eventLogEntryType, id);
            evLog.Close();
        }
        protected override void Append(LoggingEvent loggingEvent)
        {
            //
            // Write the resulting string to the event log system
            //
            int eventID = 0;

            // Look for the EventLogEventID property
            object eventIDPropertyObj = loggingEvent.LookupProperty("EventID");

            if (eventIDPropertyObj != null)
            {
                if (eventIDPropertyObj is int)
                {
                    eventID = (int)eventIDPropertyObj;
                }
                else
                {
                    string eventIDPropertyString = eventIDPropertyObj as string;
                    if (eventIDPropertyString != null && eventIDPropertyString.Length > 0)
                    {
                        // Read the string property into a number
                        int intVal;
                        if (SystemInfo.TryParse(eventIDPropertyString, out intVal))
                        {
                            eventID = intVal;
                        }
                        else
                        {
                            ErrorHandler.Error("Unable to parse event ID property [" + eventIDPropertyString + "].");
                        }
                    }
                }
            }

            // Write to the event log
            try
            {
                string eventTxt = RenderLoggingEvent(loggingEvent);

                // There is a limit of 32K characters for an event log message
                if (eventTxt.Length > 32000)
                {
                    eventTxt = eventTxt.Substring(0, 32000);
                }

                EventLogEntryType entryType = GetEntryType(loggingEvent.Level);

                using (SecurityContext.Impersonate(this))
                {
                    EventLogPermission eventLogPermission = new EventLogPermission(EventLogPermissionAccess.Administer, ".");

                    eventLogPermission.PermitOnly();

                    EventLog.WriteEntry(ApplicationName, eventTxt, entryType, eventID);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.Error("Unable to write to event log [" + LogName + "] using source [" + ApplicationName + "]", ex);
            }
        }