[英]log4net accessing context properties from custom appender
我正在使用log4net和一個自定義appender,它會記錄到Azure存儲表,並且在我的Appender中訪問log4net.GlobalContext
屬性時遇到問題。 我從Azure函數內部進行日志記錄。
附加是非常簡單和簡單的工作正常,但我想添加一些自定義屬性 - 其中一個是InvocationId,只能從Azure功能訪問,所以我想知道實現這一目標的最佳方法。
知道我錯過了什么嗎?
追加程序
protected override void Append(LoggingEvent loggingEvent)
{
_tableCtx.Log(new LogEntry
{
Timestamp = loggingEvent.TimeStamp,
Message = loggingEvent.RenderedMessage,
Level = loggingEvent.Level.Name,
// properties is always empty
InvocationId = loggingEvent.Properties["InvocationId"],
PartitionKey = loggingEvent.LoggerName,
});
}
Azure功能
public static async Task<HttpResponseMessage> MyFunction([HttpTrigger(AuthorizationLevel.Function, "post")]HttpRequestMessage req, ILogger log, ExecutionContext context)
{
log4net.GlobalContext.Properties["InvocationId"] = context.InvocationId;
using (var config = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyFunc.logging.config"))
{
log4net.Config.XmlConfigurator.Configure(config);
}
var log = LogManager.GetLogger(context.FunctionName);
}
嘗試
loggingEvent.GetProperties()["InvocationId"]
要么
loggingEvent.LookupProperty("InvocationId")
有關詳細信息,請參見此處
初始化log4net后,您必須設置該屬性:
using (var config = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyFunc.logging.config"))
{
log4net.Config.XmlConfigurator.Configure(config);
}
var log = LogManager.GetLogger(context.FunctionName);
log4net.GlobalContext.Properties["InvocationId"] = context.InvocationId;
您還必須在尚未初始化時初始化log4net。 每次通話都這樣做效率不高。
我最終做到了這一點,這比我想象的要簡單。
log4net.LogicalThreadContext.Properties["InvocationId"].ToString()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.