您应该能够覆盖 System.Diagnostics.XmlWriterTraceListener 类中的写入方法,并将您的新类用作跟踪侦听器。
public class TestPrettyPrint : System.Diagnostics.XmlWriterTraceListener
{
public TestPrettyPrint(Stream stream) : base(stream) { }
public TestPrettyPrint(string filename) : base(filename) { }
public TestPrettyPrint(TextWriter tw) : base(tw) { }
public TestPrettyPrint(Stream stream, string filename) : base(stream, filename) { }
public TestPrettyPrint(string filename, string name) : base(filename, name) { }
public TestPrettyPrint(TextWriter tw, string name) : base(tw, name) { }
public override void Write(string message)
{
message = PrettyPrintMessage(message);
base.Write(message);
}
public override void Write(string message, string category)
{
message = PrettyPrintMessage(message);
base.Write(message, category);
}
public override void WriteLine(string message)
{
message = PrettyPrintMessage(message);
base.WriteLine(message);
}
public override void WriteLine(string message, string category)
{
message = PrettyPrintMessage(message);
base.WriteLine(message, category);
}
private string PrettyPrintMessage(string messageToPP)
{
try
{
XDocument doc = XDocument.Parse(messageToPP);
return doc.ToString();
}
catch (Exception)
{
return messageToPP;
}
}
}
https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/tracing-and-message-logging
然后在 web.config 中设置跟踪侦听器时将 System.Diagnostics.XmlWriterTraceListener 替换为 MyNamespace.TestPrettyPrint。
然后,您可以在 filebeat 中设置多行探查器以将信息发送到 logstash。