I had the same issue and I've managed to achieve this by slightly modifying @Kantium answer.
While creating the PublisherKey the code was creating a SubKey "ChannelReference", the correct SubKey name would be "ChannelReferences".
I can't add a comment to his reply because of reputation requirements, so I'll just add fixed code here:
static string EVTXAppName = "MyEventLog";
static string EVTXLogName = "Operational";
static string EVTXLogGUID = "{004995f9-47e7-4c6d-ad51-6ae6c06faddd}";
static string FilePath;
static string FolderName;
// static string EVTXFilePath;
static RegistryKey localKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
static RegistryKey _FilePathKey;
static RegistryKey _FolderNameKey;
static RegistryKey _PublisherKey;
static WindowsEventLogHelper()
{
FilePath = EVTXAppName + "/" + EVTXLogName;
FolderName = EVTXAppName;
// EVTXFilePath = @"%SystemRoot%\System32\Winevt\Logs\" + FolderName + "%4" + EVTXLogName + ".evtx";
RegistryKey _FilePath = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Winevt\Channels", true);
RegistryKey _FolderName = localKey32.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog\Application", true);
RegistryKey _Publisher = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers", true);
_FilePathKey = _FilePath.OpenSubKey(FilePath, true);
_FolderNameKey = _FolderName.OpenSubKey(FolderName, true);
_PublisherKey = _Publisher.OpenSubKey(EVTXLogGUID, true);
}
public static bool Exist()
{
RegistryKey localTestKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey _LocationTestOne = localTestKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Winevt\Channels", true);
RegistryKey _LocationTestTwo = localTestKey32.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog\Application", true);
RegistryKey _PublisherTest = localTestKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers", true);
RegistryKey _FilePathTestKey = _LocationTestOne.OpenSubKey(FilePath, true);
RegistryKey _FolderNameTestKey = _LocationTestTwo.OpenSubKey(FolderName, true);
RegistryKey _PublisherTestKey = _PublisherTest.OpenSubKey(EVTXLogGUID, true);
return (_PublisherTestKey != null && _FolderNameTestKey != null && _FilePathTestKey != null);
}
public static void Delete()
{
RegistryKey localTestKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey _LocationTestOne = localTestKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Winevt\Channels", true);
RegistryKey _LocationTestTwo = localTestKey32.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog\Application", true);
RegistryKey _PublisherTest = localTestKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers", true);
_LocationTestOne.DeleteSubKeyTree(FilePath, true);
_LocationTestTwo.DeleteSubKeyTree(FolderName, true);
_PublisherTest.DeleteSubKeyTree(EVTXLogGUID, true);
}
public static bool BuildAllTree()
{
if (_FilePathKey == null)
{
RegistryKey _FilePath = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Winevt\Channels", true);
_FilePathKey = _FilePath.CreateSubKey(FilePath);
}
_FilePathKey.SetValue("Enabled", 1, RegistryValueKind.DWord);
_FilePathKey.SetValue("Type", 1, RegistryValueKind.DWord);
_FilePathKey.SetValue("Isolation", 0, RegistryValueKind.DWord);
_FilePathKey.SetValue("RestrictGuestAccess", "1", RegistryValueKind.String);
_FilePathKey.SetValue("Retention", 0, RegistryValueKind.DWord);
_FilePathKey.SetValue("ChannelAccess", "O:BAG:SYD:(A;;0x2;;;S-1-15-2-1)(A;;0xf0007;;;SY)(A;;0x7;;;BA)(A;;0x7;;;SO)(A;;0x3;;;IU)(A;;0x3;;;SU)(A;;0x3;;;S-1-5-3)(A;;0x3;;;S-1-5-33)(A;;0x1;;;S-1-5-32-573)", RegistryValueKind.String);
_FilePathKey.SetValue("OwningPublisher", EVTXLogGUID, RegistryValueKind.String);
_FilePathKey.SetValue("MaxSize", 541589504, RegistryValueKind.DWord); //Decimal 512Mo
_FilePathKey.SetValue("MaxSizeUpper", 0, RegistryValueKind.DWord);
if (_FolderNameKey == null)
{
RegistryKey _FolderName = localKey32.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog\Application", true);
_FolderNameKey = _FolderName.CreateSubKey(FolderName);
}
_FolderNameKey.SetValue("ProviderGuid", EVTXLogGUID, RegistryValueKind.String);
// _FolderNameKey.SetValue("File", EVTXFilePath, RegistryValueKind.ExpandString);
if (_PublisherKey == null)
{
RegistryKey _Publisher = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers", true);
_PublisherKey = _Publisher.CreateSubKey(EVTXLogGUID);
RegistryKey _PublisherChannelReferenceKey = _PublisherKey.OpenSubKey("ChannelReferences", true);
if (_PublisherChannelReferenceKey == null)
{
_PublisherChannelReferenceKey = _PublisherKey.CreateSubKey("ChannelReferences");
RegistryKey _Publisher0Key = _PublisherChannelReferenceKey.OpenSubKey("0", true);
if (_Publisher0Key == null)
{
_Publisher0Key = _PublisherChannelReferenceKey.CreateSubKey("0");
}
_Publisher0Key.SetValue("Flags", 0, RegistryValueKind.DWord);
_Publisher0Key.SetValue("Id", 16, RegistryValueKind.DWord);
_Publisher0Key.SetValue("", FilePath, RegistryValueKind.String);
}
_PublisherChannelReferenceKey.SetValue("Count", 1, RegistryValueKind.DWord);
}
_PublisherKey.SetValue("Enabled", 1, RegistryValueKind.DWord);
_PublisherKey.SetValue("", FolderName, RegistryValueKind.String);
return Exist() == true;
}
Edit: the evtx file path seems to be not mandatory, as it gets created by windows, so I've commented it out here. If anyone finds it useful then just uncomment it. I've removed it so that I can re use the code (minus the creation of the "FolderKey") to add other entries into the folder.