本文整理汇总了C#中System.Diagnostics.EventLog.WriteEvent方法的典型用法代码示例。如果您正苦于以下问题:C# EventLog.WriteEvent方法的具体用法?C# EventLog.WriteEvent怎么用?C# EventLog.WriteEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Diagnostics.EventLog
的用法示例。
在下文中一共展示了EventLog.WriteEvent方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
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();
}
示例2: LogEvent
/// <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();
}
示例3: WriteEvent2_Values_Null
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");
}
}
示例4: WriteEvent2_Source_DoesNotExist
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");
}
}
示例5: WriteEvent2_Source_Empty
[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");
}
}
示例6: WriteEvent2_Instance_Null
public void WriteEvent2_Instance_Null ()
{
using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
eventLog.WriteEvent (null, new byte [0], "replace");
}
}
示例7: WriteEvent2_Data_Null
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");
}
}
示例8: WriteEvent2
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");
}
}
示例9: WriteEvent
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);
}
}
示例10: WriteToEventLog
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;
}
}