示例#1
0
 public static void WriteEvent(string source, EventInstance instance, byte [] data, params object [] values)
 {
     using (EventLog eventLog = new EventLog()) {
         eventLog.Source = source;
         eventLog.WriteEvent(instance, data, values);
     }
 }
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType severity, int id, string format, params object[] args)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, format, args, null, null))
            {
                return;
            }

            EventInstance data = CreateEventInstance(severity, id);

            if (args == null || args.Length == 0)
            {
                EventLog.WriteEvent(data, format);
            }
            else if (string.IsNullOrEmpty(format))
            {
                string[] strings = new string[args.Length];
                for (int i = 0; i < args.Length; i++)
                {
                    strings[i] = args[i].ToString();
                }

                EventLog.WriteEvent(data, strings);
            }
            else
            {
                EventLog.WriteEvent(data, string.Format(CultureInfo.InvariantCulture, format, args));
            }
        }
示例#3
0
 public void WriteEntry(string message, EventLogEntryType type, int eventID, short category)
 {
     try
     {
         if (_enabled)
         {
             DiagnosticsEventLog.WriteEvent(new EventInstance(eventID, category, type), message);
         }
     }
     catch (SecurityException sx)
     {
         _enabled = false;
         // We couldn't create the log or source name. Disable logging.
         try
         {
             using (var backupLog = new System.Diagnostics.EventLog("Application", ".", "Application"))
             {
                 backupLog.WriteEvent(new EventInstance(instanceId: 0, categoryId: 0, EventLogEntryType.Error),
                                      $"Unable to log .NET application events. {sx.Message}");
             }
         }
         catch (Exception)
         {
         }
     }
 }
示例#4
0
        static void Main(string[] args)
        {
            //
            // New-EventLog -source "MySource" -logname "MyLog" (cf. https://technet.microsoft.com/en-us/library/hh849768.aspx)
            // Run eventvwr
            // Remove-EventLog -logname "MyLog" (cf. https://technet.microsoft.com/en-us/library/hh849786.aspx)
            //


            WriteLine("Option A");
            var eventLog = new EventLog() {Source = "Application"};
            eventLog.WriteEntry("Message Option A");

            WriteLine("Option B");
            var eventLogB = new EventLog() {Source = "MyLog", EnableRaisingEvents = true};
            var eventInstanceB = new EventInstance(0, 1);
            eventLogB.WriteEvent(eventInstanceB, "Message Option B");

            WriteLine("Option C");
            var eventLogC = new EventLog() {Source = "MyLog"};
            eventLogC.WriteEntry("Message Option C", EventLogEntryType.Error);

            WriteLine("Option D");
            var eventLogD = new EventLog() {Source = "MySource", EnableRaisingEvents = true};
            eventLogD.WriteEntry("Message Option D", EventLogEntryType.Error);

            WriteLine("Done.");
            ReadLine();
        }
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, params object[] data)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, null, null, null, data))
            {
                return;
            }

            EventInstance inst = CreateEventInstance(severity, id);

            var sb = new StringBuilder();

            if (data != null)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(", ");
                    }

                    if (data[i] != null)
                    {
                        sb.Append(data[i].ToString());
                    }
                }
            }

            EventLog.WriteEvent(inst, new object[] { sb.ToString() });
        }
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType severity, int id, object data)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, null, null, data, null))
            {
                return;
            }

            EventLog.WriteEvent(CreateEventInstance(severity, id), new object[] { data });
        }
        public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType severity, int id, string message)
        {
            if (Filter != null && !Filter.ShouldTrace(eventCache, source, severity, id, message, null, null, null))
            {
                return;
            }

            EventLog.WriteEvent(CreateEventInstance(severity, id), message);
        }
示例#8
0
        /// <summary>
        /// Logs message to Windows Event Log. If bool "isError" passed as true, aborts service.
        /// </summary>
        /// <param name="message">Message that will be written to Event Log</param>
        /// <param name="isError">if true, sets event as error and aborts thread</param>
        public static void LogEvent(string message, bool isError)
        {
            EventLog eventLog = new EventLog();

            // Check if the event source exists. If not create it.
            if (!EventLog.SourceExists("EphonetablesSource"))
            {
                EventLog.CreateEventSource("EphonetablesSource", "EphoneTablesLog");
            }

            // Set the source name for writing log entries.
            eventLog.Source = "EphonetablesSource";

            // Create an event ID to add to the event log
            if (!isError)
            {
                eventLog.WriteEvent(new EventInstance(8, 4, EventLogEntryType.Information), message);
            }
            else
            {
                eventLog.WriteEvent(new EventInstance(8, 4, EventLogEntryType.Error), message);
            }
            eventLog.Close();
        }
		public void WriteEvent2_Values_Null ()
		{
			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
					byte [] data = new byte [] { 23, 54 };
					EventInstance instance = new EventInstance (5, 556,
						EventLogEntryType.Warning);
					eventLog.WriteEvent (instance, data, (object) null);

					// MSBUG: Assert.AreEqual (1, eventLog.Entries.Count, "#A1");
					Assert.AreEqual ("monologtemp", eventLog.Log, "#A2");
					Assert.AreEqual ("monotempsource", eventLog.Source, "#A3");
					Assert.IsTrue (EventLog.Exists ("monologtemp"), "#A4");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource"), "#A5");
					Assert.IsTrue (EventLog.SourceExists ("monologtemp"), "#A6");
					Assert.AreEqual ("monologtemp", EventLog.LogNameFromSourceName ("monotempsource", "."), "#A7");

					EventLogEntry entry = eventLog.Entries [eventLog.Entries.Count - 1];
					Assert.IsNotNull (entry, "#B1");
					Assert.IsNotNull (entry.Category, "#B2");
					Assert.AreEqual ("(556)", entry.Category, "#B3");
					Assert.AreEqual (556, entry.CategoryNumber, "#B4");
					Assert.IsNotNull (entry.Data, "#B5");
					Assert.AreEqual (data, entry.Data, "#B6");
					Assert.AreEqual (EventLogEntryType.Warning, entry.EntryType, "#B7");
					Assert.AreEqual (5, entry.EventID, "#B8");
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#B9");
					Assert.IsNotNull (entry.MachineName, "#B10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#B11");
					Assert.IsNotNull (entry.ReplacementStrings, "#B12");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#B13");
					Assert.IsNotNull (entry.Source, "#B14");
					Assert.AreEqual ("monotempsource", entry.Source, "#B15");
					Assert.IsNull (entry.UserName, "#B16");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
		public void WriteEvent2_Source_DoesNotExist ()
		{
			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				return;

			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.SourceExists ("monoothersource", "."))
				Assert.Ignore ("Event log source 'monoothersource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monoothersource", "monologtemp");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
					byte [] data = new byte [] { 23, 54 };
					EventInstance instance = new EventInstance (5, 1,
						EventLogEntryType.Error);
					eventLog.WriteEvent (instance, data, "replace1", "replace2");

					// MSBUG: Assert.AreEqual (1, eventLog.Entries.Count, "#A1");
					Assert.AreEqual ("monologtemp", eventLog.Log, "#A2");
					Assert.AreEqual ("monotempsource", eventLog.Source, "#A3");
					Assert.IsTrue (EventLog.Exists ("monologtemp"), "#A4");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource"), "#A5");
					Assert.IsTrue (EventLog.SourceExists ("monologtemp"), "#A6");
					Assert.AreEqual ("monologtemp", EventLog.LogNameFromSourceName ("monotempsource", "."), "#A7");

					EventLogEntry entry = eventLog.Entries [eventLog.Entries.Count - 1];
					Assert.IsNotNull (entry, "#B1");
					Assert.IsNotNull (entry.Category, "#B2");
					Assert.AreEqual ("(1)", entry.Category, "#B3");
					Assert.AreEqual (1, entry.CategoryNumber, "#B4");
					Assert.IsNotNull (entry.Data, "#B5");
					Assert.AreEqual (data, entry.Data, "#B6");
					Assert.AreEqual (EventLogEntryType.Error, entry.EntryType, "#B7");
					Assert.AreEqual (5, entry.EventID, "#B8");
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#B9");
					Assert.IsNotNull (entry.MachineName, "#B10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#B11");
					Assert.IsNotNull (entry.ReplacementStrings, "#B12");
					Assert.AreEqual (2, entry.ReplacementStrings.Length, "#B13");
					Assert.AreEqual ("replace1", entry.ReplacementStrings [0], "#B14");
					Assert.AreEqual ("replace2", entry.ReplacementStrings [1], "#B15");
					Assert.IsNotNull (entry.Source, "#B16");
					Assert.AreEqual ("monotempsource", entry.Source, "#B17");
					Assert.IsNull (entry.UserName, "#B18");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
		[ExpectedException (typeof (ArgumentException))] // Source property was not set before writing to the event log
		public void WriteEvent2_Source_Empty ()
		{
			using (EventLog eventLog = new EventLog ("monologtemp")) {
				EventInstance instance = new EventInstance (5, 1,
					EventLogEntryType.Information);
				eventLog.WriteEvent (instance, new byte [0], "replace");
			}
		}
		public void WriteEvent2_Instance_Null ()
		{
			using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
				eventLog.WriteEvent (null, new byte [0], "replace");
			}
		}
		public void WriteEvent2_Data_Null ()
		{
			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
					EventInstance instance = new EventInstance (5, 444,
						EventLogEntryType.Warning);
					eventLog.WriteEvent (instance, null, "replace1", null, "replace3");

					// MSBUG: Assert.AreEqual (1, eventLog.Entries.Count, "#A1");
					Assert.AreEqual ("monologtemp", eventLog.Log, "#A2");
					Assert.AreEqual ("monotempsource", eventLog.Source, "#A3");
					Assert.IsTrue (EventLog.Exists ("monologtemp"), "#A4");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource"), "#A5");
					Assert.IsTrue (EventLog.SourceExists ("monologtemp"), "#A6");
					Assert.AreEqual ("monologtemp", EventLog.LogNameFromSourceName ("monotempsource", "."), "#A7");

					EventLogEntry entry = eventLog.Entries [eventLog.Entries.Count - 1];
					Assert.IsNotNull (entry, "#B1");
					Assert.IsNotNull (entry.Category, "#B2");
					Assert.AreEqual ("(444)", entry.Category, "#B3");
					Assert.AreEqual (444, entry.CategoryNumber, "#B4");
					Assert.IsNotNull (entry.Data, "#B5");
					Assert.AreEqual (0, entry.Data.Length, "#B6");
					Assert.AreEqual (EventLogEntryType.Warning, entry.EntryType, "#B7");
					Assert.AreEqual (5, entry.EventID, "#B8");
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#B9");
					Assert.IsNotNull (entry.MachineName, "#B10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#B11");
					Assert.IsNotNull (entry.ReplacementStrings, "#B12");
					Assert.AreEqual (3, entry.ReplacementStrings.Length, "#B13");
					Assert.AreEqual ("replace1", entry.ReplacementStrings [0], "#B14");
					Assert.AreEqual (string.Empty, entry.ReplacementStrings [1], "#B15");
					Assert.AreEqual ("replace3", entry.ReplacementStrings [2], "#B16");
					Assert.IsNotNull (entry.Source, "#B17");
					Assert.AreEqual ("monotempsource", entry.Source, "#B18");
					Assert.IsNull (entry.UserName, "#B19");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
		public void WriteEvent2 ()
		{
			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
					byte [] data = new byte [] { 23, 54 };
					EventInstance instance = new EventInstance (5, 666,
						EventLogEntryType.FailureAudit);
					eventLog.WriteEvent (instance, data, 5, "new" + Environment.NewLine + "line", true, null);

					// MSBUG: Assert.AreEqual (1, eventLog.Entries.Count, "#A1");
					Assert.AreEqual ("monologtemp", eventLog.Log, "#A2");
					Assert.AreEqual ("monotempsource", eventLog.Source, "#A3");
					Assert.IsTrue (EventLog.Exists ("monologtemp"), "#A4");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource"), "#A5");
					Assert.IsTrue (EventLog.SourceExists ("monologtemp"), "#A6");
					Assert.AreEqual ("monologtemp", EventLog.LogNameFromSourceName ("monotempsource", "."), "#A7");

					EventLogEntry entry = eventLog.Entries [eventLog.Entries.Count - 1];
					Assert.IsNotNull (entry, "#B1");
					Assert.IsNotNull (entry.Category, "#B2");
					Assert.AreEqual ("(666)", entry.Category, "#B3");
					Assert.AreEqual (666, entry.CategoryNumber, "#B4");
					Assert.IsNotNull (entry.Data, "#B5");
					Assert.AreEqual (data, entry.Data, "#B6");
					Assert.AreEqual (EventLogEntryType.FailureAudit, entry.EntryType, "#B7");
					Assert.AreEqual (5, entry.EventID, "#B8");
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#B9");
					Assert.IsNotNull (entry.MachineName, "#B10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#B11");
					Assert.IsNotNull (entry.ReplacementStrings, "#B12");
					Assert.AreEqual (4, entry.ReplacementStrings.Length, "#B13");
					Assert.AreEqual ("5", entry.ReplacementStrings [0], "#B14");
					Assert.AreEqual ("new" + Environment.NewLine + "line", entry.ReplacementStrings [1], "#B15");
					Assert.AreEqual (true.ToString (), entry.ReplacementStrings [2], "#B16");
					Assert.AreEqual (string.Empty, entry.ReplacementStrings [3], "#B17");
					Assert.IsNotNull (entry.Source, "#B18");
					Assert.AreEqual ("monotempsource", entry.Source, "#B19");
					Assert.IsNull (entry.UserName, "#B20");

					eventLog.WriteEvent (instance, data);

					entry = eventLog.Entries [eventLog.Entries.Count - 1];
					Assert.IsNotNull (entry, "#C1");
					Assert.IsNotNull (entry.Category, "#C2");
					Assert.AreEqual ("(666)", entry.Category, "#C3");
					Assert.AreEqual (666, entry.CategoryNumber, "#C4");
					Assert.IsNotNull (entry.Data, "#C5");
					Assert.AreEqual (data, entry.Data, "#C6");
					Assert.AreEqual (EventLogEntryType.FailureAudit, entry.EntryType, "#C7");
					Assert.AreEqual (5, entry.EventID, "#C8");
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#C9");
					Assert.IsNotNull (entry.MachineName, "#C10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#C11");
					Assert.IsNotNull (entry.ReplacementStrings, "#C12");
					Assert.AreEqual (0, entry.ReplacementStrings.Length, "#C13");
					Assert.IsNotNull (entry.Source, "#C14");
					Assert.AreEqual ("monotempsource", entry.Source, "#C15");
					Assert.IsNull (entry.UserName, "#C16");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
示例#15
0
		public static void WriteEvent (string source, EventInstance instance, byte [] data, params object [] values)
		{
			using (EventLog eventLog = new EventLog ()) {
				eventLog.Source = source;
				eventLog.WriteEvent (instance, data, values);
			}
		}
示例#16
0
        private bool WriteToEventLog(string logName, string eventSource, int eventId, EventLogEntryType eventLevel, EventCategory eventCategory, object[] insertionStrings)
        {
            try
            {
                using (EventLog operationalLog = new EventLog(logName))
                {
                    operationalLog.Source = eventLogSource;
                    operationalLog.WriteEvent(new EventInstance(eventId, (int)eventCategory, eventLevel), insertionStrings);
                }

                return true;
            }
            catch
            {
                //TODO: could not log to event log. Do we need to do anything?
                return false;
            }
        }