如何将eventLog.Properties转换为XML?

时间:2018-07-10 13:26:40

标签: xml collections serilog

我想通过存储过程将Serilog消息存储到数据库中。我创建了新的接收器。

    namespace Copacking.Web.Logging
    {
        class StoredProcedureSink : ILogEventSink
        {
            private readonly string connectionString;
            public StoredProcedureSink(string connectionString) {
                this.connectionString = connectionString;
            }

            public void Emit(LogEvent logEvent)
            {
var xml = new XElement("properties");

            foreach (KeyValuePair<string, LogEventPropertyValue> property in logEvent.Properties)
            {
                xml.Add(new XElement("property", new XAttribute("Key", property.Key), property.Value));
            }

                using (var conn = new SqlConnection(connectionString))
                using (var command = new SqlCommand("dbo.usp_insertLog", conn){ CommandType = CommandType.StoredProcedure })
                {
                    conn.Open();
                    command.Parameters.Add(new SqlParameter("@message", logEvent.RenderMessage()));
                    command.Parameters.Add(new SqlParameter("@messageTemplate", logEvent.MessageTemplate.Text));
                    command.Parameters.Add(new SqlParameter("@level", logEvent.Level.ToString()));
                    command.Parameters.Add(new SqlParameter("@timestamp", logEvent.Timestamp));
                    command.Parameters.Add(new SqlParameter("@exception", logEvent.Exception.ToString()));
                    command.Parameters.Add(new SqlParameter("@properties", xml.ToString()));
                    command.ExecuteNonQuery();
                }
            }

        }

    } 

它有效,但我得到

 <properties>
  <property Key="SourceContext">"..."</property>
  <property Key="RequestId">"..."</property>
  <property Key="RequestPath">"..."</property>
  <property Key="RequestHeader">[("Connection": "Keep-Alive"), ("Accept": "text/html, application/xhtml+xml, image/jxr, */*"), ("Accept-Encoding": "..."), ("Accept-Language": "..."), ("Host": "..."), ("User-Agent": "..."), ("MS-ASPNETCORE-TOKEN": "..."), ("MS-ASPNETCORE-WINAUTHTOKEN": "..."), ("X-Original-Proto": "..."), ("X-Original-For": "...")]</property>
  <property Key="ActivityId">"..."</property>
  <property Key="UserName">"..."</property>
</properties>

代替

<properties>
  <property key="SourceContext">...</property>
  <property key="RequestId">...</property>
  <property key="RequestPath">...</property>
  <property key="RequestHeader">
    <dictionary>
      <item key="Connection">...</item>
      <item key="Accept">...</item>
      <item key="Accept-Encoding">...</item>
      <item key="Accept-Language">...</item>
      <item key="Host">...</item>
      <item key="User-Agent">...</item>
      <item key="MS-ASPNETCORE-TOKEN">...</item>
      <item key="MS-ASPNETCORE-WINAUTHTOKEN">...</item>
      <item key="X-Original-Proto">...</item>
      <item key="X-Original-For">...</item>
    </dictionary>
  </property>
  <property key="ActivityId">...</property>
  <property key="UserName">...</property>
</properties>

任何想法如何更改代码?也许实现一些递归?!

1 个答案:

答案 0 :(得分:0)

我几乎没有解决办法。我复制了以下课程

  • ColumnOptions
  • StandardColumn
  • XmlPropertyFormatter

来自serilog nuget软件包https://github.com/serilog/serilog-sinks-mssqlserver 并称为

command.Parameters.Add(new SqlParameter("@properties", ConvertPropertiesToXmlStructure(logEvent.Properties)));

我不能直接使用nuget包中的类,因为XmlPropertyFormatter是内部的,因此只能在包内部调用。