【发布时间】:2017-12-14 17:17:25
【问题描述】:
我正在使用带有自定义附加程序的 log4net,该附加程序记录到 Azure 存储表,并且在我的附加程序中访问 log4net.GlobalContext 属性时遇到问题。我正在从 Azure 函数内部进行日志记录。
附加非常简单明了,工作正常,但我想添加几个自定义属性 - 其中一个是 InvocationId 并且只能从 Azure Functions 访问,所以我想知道最好的方法实现这一目标。
知道我错过了什么吗?
附加器
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);
}
【问题讨论】:
-
看起来在 appender xml 文件中做这种工作可能更容易?见stackoverflow.com/questions/28775568/…。
标签: c# azure log4net azure-functions