Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<ItemGroup>
<PackageReference Include="System.Diagnostics.EventLog" Version="6.0.0" />
</ItemGroup>

</Project>
44 changes: 18 additions & 26 deletions snippets/csharp/System.Diagnostics/EventLog/Overview/source.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
// <Snippet1>
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

public static void Main(){

// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource"))
{
//An event log source should not be created and immediately used.
//There is a latency time to enable the source, it should be created
//prior to executing the application that uses the source.
//Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}

// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";

// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
}
// Create the source if it doesn't already exist.
if (!EventLog.SourceExists("MySource"))
{
// An event log source shouldn't be created and immediately used.
// The source takes time to become enabled.
// Create it before executing the application that uses it.
// Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatedEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}

// Create an EventLog instance and assign its source.
using EventLog myLog = new();
myLog.Source = "MySource";

// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
<ItemGroup>
<PackageReference Include="System.Diagnostics.EventLog" Version="6.0.0" />
</ItemGroup>

</Project>
47 changes: 19 additions & 28 deletions snippets/csharp/System.Diagnostics/EventLog/Source/source.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
// <Snippet1>
using System;
using System.Diagnostics;
using System.Threading;

class MySample{

public static void Main(){

// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource"))
{
// An event log source should not be created and immediately used.
// There is a latency time to enable the source, it should be created
// prior to executing the application that uses the source.
// Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}

// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
myLog.Source = "MySource";

// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");

Console.WriteLine("Message written to event log.");
}
// Create the source if it doesn't already exist.
if (!EventLog.SourceExists("MySource"))
{
// An event log source shouldn't be created and immediately used.
// The source takes time to become enabled.
// Create it before executing the application that uses it.
// Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "MyNewLog");
Console.WriteLine("CreatingEventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}

// Create an EventLog instance and assign its source.
using EventLog myLog = new();
myLog.Source = "MySource";

// Write an informational entry to the event log.
myLog.WriteEntry("Writing to event log.");
Console.WriteLine("Message written to event log.");
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Diagnostics;

bool sourceExists = EventLog.SourceExists("MySource");
MySample.Run();
if (!sourceExists)
{
return;
}

MyEventLog.Run();
EventLog_WriteEntry_4.Run();
EventLog_WriteEntry_5.Run();
MySample1.Run();
MySample2.Run();
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<StartupObject>MySample</StartupObject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.EventLog" Version="6.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,52 @@

class MyEventLog
{
public static void Main()
public static void Run()
{

// <Snippet1>
int myEventID = 20;
short myCategory = 10;
// Write an informational entry to the event log.
Console.WriteLine("Write from first source ");
EventLog.WriteEntry("FirstSource", "Writing warning to event log.",
EventLogEntryType.Information, myEventID, myCategory);
EventLog.WriteEntry(
"FirstSource",
"Writing warning to event log.",
EventLogEntryType.Information,
myEventID,
myCategory);
// </Snippet1>

// <Snippet2>
//Create a byte array for binary data to associate with the entry.
// Create a byte array for binary data to associate with the entry.
byte[] myByte = new byte[10];
//Populate the byte array with simulated data.
for (int i = 0; i < 10; i++)
// Populate the byte array with simulated data.
for (int i = 0; i < myByte.Length; i++)
{
myByte[i] = (byte)(i % 2);
}
// Write an entry to the event log that includes associated binary data.
Console.WriteLine("Write from second source ");
EventLog.WriteEntry("SecondSource", "Writing warning to event log.",
EventLogEntryType.Error, myEventID, myCategory, myByte);
EventLog.WriteEntry(
"SecondSource",
"Writing warning to event log.",
EventLogEntryType.Error,
myEventID,
myCategory,
myByte);
// </Snippet2>

// <Snippet3>
// Create an EventLog instance and assign its source.
EventLog myLog = new EventLog();
using EventLog myLog = new();
myLog.Source = "ThirdSource";

// Write an informational entry to the event log.
// Write a warning entry to the event log.
Console.WriteLine("Write from third source ");
myLog.WriteEntry("Writing warning to event log.",
EventLogEntryType.Warning, myEventID, myCategory);
myLog.WriteEntry(
"Writing warning to event log.",
EventLogEntryType.Warning,
myEventID,
myCategory);
// </Snippet3>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,45 @@ The following sample demonstrates the

using System;
using System.Diagnostics;

class EventLog_WriteEntry_4
{
public static void Main()
{
try
{
// <Snippet1>
// Create the source, if it does not already exist.
if(!EventLog.SourceExists("MySource"))
{
// An event log source should not be created and immediately used.
// There is a latency time to enable the source, it should be created
// prior to executing the application that uses the source.
// Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "myNewLog");
Console.WriteLine("Creating EventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}
public static void Run()
{
try
{
// <Snippet1>
// Create the source if it doesn't already exist.
if (!EventLog.SourceExists("MySource"))
{
// An event log source shouldn't be created and immediately used.
// The source takes time to become enabled.
// Create it before executing the application that uses it.
// Execute this sample a second time to use the new source.
EventLog.CreateEventSource("MySource", "myNewLog");
Console.WriteLine("Creating EventSource");
Console.WriteLine("Exiting, execute the application a second time to use the source.");
// The source is created. Exit the application to allow it to be registered.
return;
}

// Set the 'description' for the event.
string myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType.Warning;
int myApplicationEventId = 100;
// Set the description for the event.
string myMessage = "This is my event.";
EventLogEntryType myEventLogEntryType = EventLogEntryType.Warning;
int myApplicationEventId = 100;

// Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ");
EventLog.WriteEntry("MySource",myMessage,
myEventLogEntryType, myApplicationEventId);
// </Snippet1>
}
catch(Exception e)
{
Console.WriteLine("Exception:{0}",e.Message);
}
}
}
// Write the entry in the event log.
Console.WriteLine("Writing to EventLog.. ");
EventLog.WriteEntry(
"MySource",
myMessage,
myEventLogEntryType,
myApplicationEventId);
// </Snippet1>
}
catch (Exception e)
{
Console.WriteLine($"Exception:{e.Message}");
}
}
}
Loading
Loading