The above answers are fine if you just want a simple <Data Name="param1">data</Data>
structure but it falls down if you want a more complex payload, e.g. like you get in a bluescreen event:
<Data Name="BugcheckCode">4522044</Data>
<Data Name="BugcheckParameter1">0x74006e00650076</Data>
<Data Name="BugcheckParameter2">0x61007400610044</Data>
<Data Name="BugcheckParameter3">0x610044003c003e</Data>
<Data Name="BugcheckParameter4">0x4e002000610074</Data>
<Data Name="SleepInProgress">7143521</Data>
<Data Name="PowerButtonTimestamp">18577494495789157</Data>
<Data Name="BootAppStatus">6750325</Data>
<Data Name="Checkpoint">99</Data>
<Data Name="ConnectedStandbyInProgress">true</Data>
<Data Name="SystemSleepTransitionsToOn">1795187456</Data>
<Data Name="CsEntryScenarioInstanceId">0</Data>
<Data Name="BugcheckInfoFromEFI">true</Data>
I don't believe this is possible using the EventLog.WriteEntry
helper. I noticed you can do this using PowerShell though:
New-WinEvent -ProviderName Microsoft-Windows-Kernel-Power -Id $evtID -Version 5 -Payload "<EventData><Data Name=""BugcheckCode"">210</Data><Data Name=""BugcheckParameter1"">0xffffc080b5744760</Data><Data Name=""BugcheckParameter2"">0x2</Data><Data Name=""BugcheckParameter3"">0x0</Data><Data Name=""BugcheckParameter4"">0xfffff80261641530</Data><Data Name=""SleepInProgress"">0</Data><Data Name=""PowerButtonTimestamp"">0</Data><Data Name=""BootAppStatus"">0</Data><Data Name=""Checkpoint"">0</Data><Data Name=""ConnectedStandbyInProgress"">false</Data><Data Name=""SystemSleepTransitionsToOn"">0</Data><Data Name=""CsEntryScenarioInstanceId"">0</Data><Data Name=""BugcheckInfoFromEFI"">false</Data><Data Name=""CheckpointStatus"">0</Data></EventData>"
So I dug out the dll that this uses and reflected the code out. I ended up with a little helper class that allows you to pass whatever payload you require:
public class EventLogHelper
{
/// <summary>
/// Taken from the source code of Microsoft.PowerShell.Commands.NewWinEventCommand
/// </summary>
/// <param name="providerName">"Microsoft-Windows-Kernel-Power"</param>
/// <param name="eventId">41</param>
/// <param name="version">5</param>
/// <param name="payLoad"></param>
public static void AddEventToEventLog(string providerName, long eventId, int version, string payLoad = "")
{
using (ProviderMetadata providerMetaData = LoadProvider(providerName))
{
EventDescriptor eventDescriptor = LoadEventDescriptor(providerMetaData, eventId, Convert.ToByte(version));
ProcessRecord(providerMetaData, eventDescriptor, payLoad);
}
}
private static ProviderMetadata LoadProvider(string providerName)
{
using (EventLogSession eventLogSession = new EventLogSession())
{
IEnumerable<string> providers = eventLogSession.GetProviderNames().OrderBy(s => s);
foreach (string providerName2 in providers)
{
if (string.Equals(providerName2, providerName, StringComparison.OrdinalIgnoreCase))
{
return new ProviderMetadata(providerName2);
}
}
}
throw new Exception("Failed to find Microsoft-Windows-Kernel-Power provider");
}
private static EventDescriptor LoadEventDescriptor(ProviderMetadata providerMetadata, long id, byte version)
{
EventMetadata eventMetadata = providerMetadata.Events.First(f => f.Id == id && f.Version == version);
return CreateEventDescriptor(providerMetadata, eventMetadata);
}
private static EventDescriptor CreateEventDescriptor(ProviderMetadata providerMetaData, EventMetadata emd)
{
long num = 0L;
foreach (EventKeyword keyword in emd.Keywords)
{
num |= keyword.Value;
}
byte b = 0;
using (IEnumerator<EventLogLink> enumerator2 = providerMetaData.LogLinks.GetEnumerator())
{
while (enumerator2.MoveNext() && !string.Equals(enumerator2.Current.LogName, emd.LogLink.LogName, StringComparison.OrdinalIgnoreCase))
{
b = (byte)(b + 1);
}
}
int parsedId = (int)emd.Id;
if (emd.Id > ushort.MaxValue)
parsedId = (ushort)emd.Id;
return new EventDescriptor(parsedId, emd.Version, b, (byte)emd.Level.Value, (byte)emd.Opcode.Value, emd.Task.Value, num);
}
private static void ProcessRecord(ProviderMetadata providerMetadata, EventDescriptor eventDescriptor, string payload)
{
using (EventProvider eventProvider = new EventProvider(providerMetadata.Id))
{
eventProvider.WriteEvent(ref eventDescriptor, payload);
}
}
}
this can then be called:
string payload = @"<EventData><Data Name=""BugcheckCode"">210</Data><Data Name=""BugcheckParameter1"">0xffffc080b5744760</Data><Data Name=""BugcheckParameter2"">0x2</Data><Data Name=""BugcheckParameter3"">0x0</Data><Data Name=""BugcheckParameter4"">0xfffff80261641530</Data><Data Name=""SleepInProgress"">0</Data><Data Name=""PowerButtonTimestamp"">0</Data><Data Name=""BootAppStatus"">0</Data><Data Name=""Checkpoint"">0</Data><Data Name=""ConnectedStandbyInProgress"">false</Data><Data Name=""SystemSleepTransitionsToOn"">0</Data><Data Name=""CsEntryScenarioInstanceId"">0</Data><Data Name=""BugcheckInfoFromEFI"">false</Data><Data Name=""CheckpointStatus"">0</Data></EventData>";
EventLogHelper.AddEventToEventLog("Microsoft-Windows-Kernel-Power", 41, 5, payload);