是否可以使用 NLogJsonLayout將字典寫出Exception.Data為實際的 JSON,而不僅僅是用它序列化的單個屬性toString?
我能得到的最接近的是:
<attribute name='data' layout='${exception:format=data}' />
變成
"exception": {
"type": "System.IO.InvalidDataException",
"message": "Invalid PERSON identifier: xyz",
"data": "entity: PERSON;value: xyz",
"stacktrace": "..."
}
或這個
<attribute name='exception' layout='${exception:format=@}'/>
變成
"exception": "{\"Type\":\"System.IO.InvalidDataException\", \"Message\":\"Invalid PERSON identifier: xyz\", \"Data\":{\"entity\":\"PERSON\",\"value\":\"xyz\"}, \"TargetSite\":\"Void MoveNext()\", \"StackTrace\":\"...\", \"HResult\":-2146233087}"
理想情況下,我希望看到一個產生的布局配置:
"exception": {
"type": "System.IO.InvalidDataException",
"message": "Invalid PERSON identifier: xyz",
"data": {
"entity": "PERSON",
"value": "xyz",
}
"stacktrace": "..."
}
此配置將以我想要的方式輸出事件屬性:
<attribute name="properties" encode="false" >
<layout type='JsonLayout' includeAllProperties="true" maxRecursionLimit="10"/>
</attribute>
"properties": {
"controllerName": "Person",
"actionName": "Lookup"
}
但我還沒有找到對Exception.Data. 有沒有人能夠在不使用etc手動將每個可能的字典鍵作為屬性添加到配置中的情況下完成此操作?${exceptiondata:item=entity}
uj5u.com熱心網友回復:
使用format=@then 時,它將以 json 格式輸出,但是您必須使用encode="false"以避免編碼為字串屬性:
<attribute name="exception" layout="${exception:format=@}" encode="false" />
請注意,某些例外是危險的,其中資料字典列舉了整個資料庫。您可以使用RegisterObjectTransformation這些例外來覆寫轉換。
另見:https ://github.com/NLog/NLog/wiki/How-to-use-structured-logging#transform-captured-properties
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/495196.html
