1

我正在寻找使 RollingFlatFileTraceListenerData 在使用 JsonLogFormatter 时创建有效的 JSON 文档的方法。与 XML 解析器一样,RollingFlatFileTraceListenerData 仅附加项目,只是为您提供添加项目页眉和页脚的选项,而不是更高级别的文件页眉、页脚页眉和项目分隔符。我意识到我可以在事后修改文件,但我宁愿使用有效格式构建流程。使用外部进程打开活动文件是有风险的,因为如果需要打开文件以再次记录,它可能会阻止记录进程。

电流输出:

{
  "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"
  ]
}

首选:

[
 {
      "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"
      ]
    }
]
4

2 回答 2

0

开箱即用的跟踪侦听器只是附加到文件中。您将无法打开该文件并对其进行修改,因为该文件已被锁定。如果要创建格式良好的文档,则必须创建一个自定义跟踪侦听器,该侦听器创建开始标记并确保结束标记始终位于文件的底部。

于 2013-11-12T01:45:02.910 回答
0

我也试图纠正输出,但最终放弃并处理了消费方面的一切。下面是我的示例程序,它根据https://stackoverflow.com/a/26610684/4904200中的代码向垃圾邮件事件然后尝试使用它们

关键点:

  • RollingFlatFileTraceListener页眉和页脚设置为“ ”
  • 当使用我们的流设置的日志时FileShare.ReadWrite,我们不会干扰写入
  • JsonReader.SupportMultipleContent = true是什么让我们可以处理文件中的多个对象,只要它们位于不同的行上。

这使用 Microsoft Enterprise Logging Library v6 和 Newtonsoft JSON v8。

static void Main(string[] args) {
  Thread producer = new Thread(new ThreadStart(Produce));
  Thread consumer = new Thread(new ThreadStart(Consume));
  producer.Start();
  consumer.Start();
  while (true)
    Thread.Sleep(1000);
}

public static void Produce() {
  RollingFlatFileTraceListener listener = new RollingFlatFileTraceListener(
    fileName: "jsontest",
    formatter: new JsonLogFormatter(),
    header: "",
    footer: "",
    rollSizeKB: 1024);
  LoggingConfiguration configuration = new LoggingConfiguration();
  configuration.Filters.Add(new CategoryFilter("Category Filter", new string[] { "none" }, CategoryFilterMode.AllowAllExceptDenied));
  configuration.AddLogSource("Any", SourceLevels.All, true, listener);
  LogWriter writer = new LogWriter(configuration);

  while (true)
    writer.Write(DateTime.Now);
}

public static void Consume() {
  while (true) {
    try {
      using (var fileStream = File.Open("jsontest", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
        LogEntry latestEntry = ReadJson<LogEntry>(fileStream).OrderBy(x => x.TimeStamp).FirstOrDefault();
        if (latestEntry == null)
          continue;
        Console.WriteLine("Consume\t" + latestEntry.Message);
      }
    } catch (FileNotFoundException) {
      Console.WriteLine("File Not Found");
    } catch (IOException) {
      Console.WriteLine("File Is Probably Busy");
    }
  }
}


public static IEnumerable<TResult> ReadJson<TResult>(Stream stream) {
  var serializer = new JsonSerializer();
  using (var reader = new StreamReader(stream))
  using (var jsonReader = new JsonTextReader(reader)) {
    jsonReader.SupportMultipleContent = true;
    while (jsonReader.Read()) {
      yield return serializer.Deserialize<TResult>(jsonReader);
    }
  }
}
于 2016-01-24T15:08:26.293 回答