I'm looking for way make the RollingFlatFileTraceListenerData create a valid JSON document when using JsonLogFormatter. Like the XML parser, the RollingFlatFileTraceListenerData only appends items, just giving you an option to add an item header and footer, but not a higher level file header, footer header and item separator. I realize I can modify the file after the fact, but I would rather have the process build with a valid format. Opening the active file with an external process would be risky because it could block the logging process if it needed to open the file to log again.
Current Output:
{
"Message": "Log entry created using the simplest overload.",
"Categories": [
"General"
],
"Priority": -1,
"EventId": 1,
"Severity": 8,
"LoggedSeverity": "Information",
"Title": "",
"TimeStamp": "2013-11-07T20:33:38.6537773Z",
"MachineName": "Acme01",
"AppDomainName": "Acme.TestDriver.vshost.exe",
"ProcessId": "10348",
"ProcessName": "C:\\dev\\Acme.TestDriver\\bin\\Debug\\Acme.TestDriver.vshost.exe",
"ManagedThreadName": null,
"Win32ThreadId": "11204",
"ExtendedProperties": {},
"TimeStampString": "11/7/2013 8:33:38 PM",
"ActivityId": "00000000-0000-0000-0000-000000000000",
"RelatedActivityId": null,
"ErrorMessages": null,
"ActivityIdString": "00000000-0000-0000-0000-000000000000",
"CategoriesStrings": [
"General"
]
}
{
"Message": "Log entry with a single category.",
"Categories": [
"General"
],
"Priority": -1,
"EventId": 1,
"Severity": 8,
"LoggedSeverity": "Information",
"Title": "",
"TimeStamp": "2013-11-07T20:33:38.6537773Z",
"MachineName": "Acme01",
"AppDomainName": "Acme.TestDriver.vshost.exe",
"ProcessId": "10348",
"ProcessName": "C:\\dev\\Acme.TestDriver\\bin\\Debug\\Acme.TestDriver.vshost.exe",
"ManagedThreadName": null,
"Win32ThreadId": "11204",
"ExtendedProperties": {},
"TimeStampString": "11/7/2013 8:33:38 PM",
"ActivityId": "00000000-0000-0000-0000-000000000000",
"RelatedActivityId": null,
"ErrorMessages": null,
"ActivityIdString": "00000000-0000-0000-0000-000000000000",
"CategoriesStrings": [
"General"
]
}
Preferred:
[
{
"Message": "Log entry created using the simplest overload.",
"Categories": [
"General"
],
"Priority": -1,
"EventId": 1,
"Severity": 8,
"LoggedSeverity": "Information",
"Title": "",
"TimeStamp": "2013-11-07T20:33:38.6537773Z",
"MachineName": "Acme01",
"AppDomainName": "Acme.TestDriver.vshost.exe",
"ProcessId": "10348",
"ProcessName": "C:\\dev\\Acme.TestDriver\\bin\\Debug\\Acme.TestDriver.vshost.exe",
"ManagedThreadName": null,
"Win32ThreadId": "11204",
"ExtendedProperties": {},
"TimeStampString": "11/7/2013 8:33:38 PM",
"ActivityId": "00000000-0000-0000-0000-000000000000",
"RelatedActivityId": null,
"ErrorMessages": null,
"ActivityIdString": "00000000-0000-0000-0000-000000000000",
"CategoriesStrings": [
"General"
]
}
,
{
"Message": "Log entry with a single category.",
"Categories": [
"General"
],
"Priority": -1,
"EventId": 1,
"Severity": 8,
"LoggedSeverity": "Information",
"Title": "",
"TimeStamp": "2013-11-07T20:33:38.6537773Z",
"MachineName": "Acme01",
"AppDomainName": "Acme.TestDriver.vshost.exe",
"ProcessId": "10348",
"ProcessName": "C:\\dev\\Acme.TestDriver\\bin\\Debug\\Acme.TestDriver.vshost.exe",
"ManagedThreadName": null,
"Win32ThreadId": "11204",
"ExtendedProperties": {},
"TimeStampString": "11/7/2013 8:33:38 PM",
"ActivityId": "00000000-0000-0000-0000-000000000000",
"RelatedActivityId": null,
"ErrorMessages": null,
"ActivityIdString": "00000000-0000-0000-0000-000000000000",
"CategoriesStrings": [
"General"
]
}
]
I too tried to correct the output but finally gave up and handled everything on the consumption side. Below is my sample program which spams events and then attempts to consume them, based on code from https://stackoverflow.com/a/26610684/4904200
Key points:
RollingFlatFileTraceListener
Header and Footer are set to ""FileShare.ReadWrite
so we don't interfere with writingJsonReader.SupportMultipleContent = true
is what lets us handle multiple objects in a file as long as they are on separate lines.This uses Microsoft Enterprise Logging Library v6 and Newtonsoft JSON v8.