I am using log4net with a custom appender that logs to Azure Storage Tables, and am having issues accessing log4net.GlobalContext
properties from within my Appender. I am logging from inside an Azure Function.
The appending is pretty straight forward and simple and works fine, but I would like to add a couple custom properties - one of which is an InvocationId and only accessible from the Azure Functions, so i would like to know the best way to achieve this.
Any idea what I'm missing?
Appender
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 Function
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);
}
Try
loggingEvent.GetProperties()["InvocationId"]
or
loggingEvent.LookupProperty("InvocationId")
See here for more details
You have to set the Property after you initialize 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;
You also only have to initialize log4net when it is not yet initialized. Doing it on every call is not very efficient.
我最终做到了这一点,这比我想象的要简单。
log4net.LogicalThreadContext.Properties["InvocationId"].ToString()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.