I want to create a custom folder in the Application and services log area in windows event logs

%3CLINGO-SUB%20id%3D%22lingo-sub-2177362%22%20slang%3D%22en-US%22%3EI%20want%20to%20create%20a%20custom%20folder%20in%20the%20Application%20and%20services%20log%20area%20in%20windows%20event%20logs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2177362%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%20am%20trying%20to%20create%20a%20custom%20folder%20in%20the%20Application%20and%20services%20log%20area%20in%20windows%20event%20logs.%20Inside%20the%20folder%20I%20want%20to%20create%20two%20custom%20area%E2%80%99s%20ex%3A%20Area1%2C%20Area2.In%20Area1%2C%20I%20want%20to%20write%20the%20logs%20from%20one%20source%20and%20In%20Area2%2C%20I%20want%20to%20write%20logs%20from%20another%20source.%20Original%20i%20want%20to%20do%20it%20using%20.net%20c%23%20but%20as%20couldn't%20do%20it%20using%20c%23%20soI%20used%20powershell%20script%20to%20achieve%20this.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Ethe%20code%20is%20below%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24PrimaryEventKey%20%3D%20'Comp1'%0A%24ApplicationName%20%3D%20'App1'%0A%24LogName%20%3D%20TSP%0A%0A%23%20Vars()%0A%24primarylocation%20%3D%20'HKLM%3A%5CSoftware%5CMicrosoft%5CWindows%5CCurrentVersion%5CWINEVT%5CChannels'%0A%24LogName%20%3D%20%24PrimaryEventKey%20%2B%20'-'%20%2B%20%24ApplicationName%20%2B%20'-'%20%2B%20%24LogName%0A%24EventRoot%20%3D%20(Join-Path%20%24primarylocation%20%24LogName)%0A%0Aif%20(!(Test-Path%20%24EventRoot))%20%7B%0A%20%20%20%20New-Item%20-Path%20(%24secondarylocation%20%2B%20'%2F'%20%2B%20%24Logname)%0A%20%20%20%20New-ItemProperty%20-Path%20(%24secondarylocation%20%2B%20'%2F'%20%2B%20%24Logname)%20-Name%20providerGuid%20-PropertyType%20String%20-Value%20%22%7B%24(%24GUID)%7D%22%0A%0A%20%20%20%20New-Item%20-Path%20%24EventRoot%0A%20%20%20%20New-ItemProperty%20-Path%20%24EventRoot%20-Name%20Enabled%20-PropertyType%20DWord%20-Value%201%0A%20%20%20%20New-ItemProperty%20-Path%20%24EventRoot%20-Name%20Type%20-PropertyType%20DWord%20-Value%201%0A%20%20%20%20New-ItemProperty%20-Path%20%24EventRoot%20-Name%20Isolation%20-PropertyType%20DWord%20-Value%200%0A%20%20%20%20New-ItemProperty%20-Path%20%24EventRoot%20-Name%20RestrictGuestAccess%20-PropertyType%20String%20-Value%201%0A%20%20%20%20New-ItemProperty%20-Path%20%24EventRoot%20-Name%20OwningPublisher%20-PropertyType%20String%20-Value%20%22%7B%24(%24GUID)%7D%22%0A%0A%20%20%20%20%23%20See%20https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fdesktop%2Feventlog%2Feventlog-key%20for%20documentation%20on%20the%20ChannelAccess%20or%20or%20RestrictGuestAccess%20(see%3A%20RestrictGuestAccess%20%2F%20Isolation)%0A%7D%0Aelse%20%7B%0A%20%20%20%20Write-Warning%20'Event%20Log%20(Key)%20Already%20exists%20in%20registry'%0A%7D%0A%0A%23%20Write%20into%20the%20event%20log%20(Example)%0A%24eventType%20%3D%20(%5BSystem.Diagnostics.EventLogEntryType%5D%3A%3AInformation)%0A%24evt%20%3D%20New-Object%20System.Diagnostics.EventLog(%24LogName)%0A%24evt.Source%20%3D%20%22SomeSource%22%0A%24evt.WriteEntry(%22random%20message%22%2C%20%24eventType%2C%2060001)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EBy%20running%20this%20script%20I%20am%20able%20to%20create%20folder%20hierarchy%20but%26nbsp%3B%20when%20I%20go%20to%20see%20the%20event%20logs%20It%20is%26nbsp%3B%20showing%20error%20like%3A%3CBR%20%2F%3E%3CSTRONG%3E%22Event%20Viewer%20can%20open%20the%20eventlog%20or%20custom%20view%20verify%20the%20event%20log%20service%20is%20running%20or%20query%20is%20to%20long.%20the%20data%20is%20invalid(13).%22%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3EHow%20to%20resolve%20this%20issue%20or%20is%20there%20any%20alternative%20approach%20to%20do%20it%20%3FOr%20any%20other%20way%20to%20create%20folder%20structure%20and%20then%20write%20logs%20in%20.net%20or%20through%20script%20%3F%3C%2FSTRONG%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2177362%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

I am trying to create a custom folder in the Application and services log area in windows event logs. Inside the folder I want to create two custom area’s ex: Area1, Area2.In Area1, I want to write the logs from one source and In Area2, I want to write logs from another source. Original i want to do it using .net c# but as couldn't do it using c# soI used powershell script to achieve this.
the code is below:

 

 

$PrimaryEventKey = 'Comp1'
$ApplicationName = 'App1'
$LogName = TSP

# Vars()
$primarylocation = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\WINEVT\Channels'
$LogName = $PrimaryEventKey + '-' + $ApplicationName + '-' + $LogName
$EventRoot = (Join-Path $primarylocation $LogName)

if (!(Test-Path $EventRoot)) {
    New-Item -Path ($secondarylocation + '/' + $Logname)
    New-ItemProperty -Path ($secondarylocation + '/' + $Logname) -Name providerGuid -PropertyType String -Value "{$($GUID)}"

    New-Item -Path $EventRoot
    New-ItemProperty -Path $EventRoot -Name Enabled -PropertyType DWord -Value 1
    New-ItemProperty -Path $EventRoot -Name Type -PropertyType DWord -Value 1
    New-ItemProperty -Path $EventRoot -Name Isolation -PropertyType DWord -Value 0
    New-ItemProperty -Path $EventRoot -Name RestrictGuestAccess -PropertyType String -Value 1
    New-ItemProperty -Path $EventRoot -Name OwningPublisher -PropertyType String -Value "{$($GUID)}"

    # See https://docs.microsoft.com/en-us/windows/desktop/eventlog/eventlog-key for documentation on the ChannelAccess or or RestrictGuestAccess (see: RestrictGuestAccess / Isolation)
}
else {
    Write-Warning 'Event Log (Key) Already exists in registry'
}

# Write into the event log (Example)
$eventType = ([System.Diagnostics.EventLogEntryType]::Information)
$evt = New-Object System.Diagnostics.EventLog($LogName)
$evt.Source = "SomeSource"
$evt.WriteEntry("random message", $eventType, 60001)

 

 

 

By running this script I am able to create folder hierarchy but  when I go to see the event logs It is  showing error like:
"Event Viewer can open the eventlog or custom view verify the event log service is running or query is to long. the data is invalid(13)."

How to resolve this issue or is there any alternative approach to do it ?Or any other way to create folder structure and then write logs in .net or through script ?

0 Replies