c# - log4net 从自定义 appender 访问上下文属性

标签 c# azure log4net azure-functions

我将 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);
}

最佳答案

尝试

loggingEvent.GetProperties()["InvocationId"]

loggingEvent.LookupProperty("InvocationId")

参见 here了解更多详情

关于c# - log4net 从自定义 appender 访问上下文属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47818871/

相关文章:

c# - 如何在C#中让鼠标按住右键单击

azure - 访问 Azure Pipelines 中的私有(private) Nuget 服务器中托管的 Nuget 包

log4net - log4net 中的 AdoNetAppender 不记录或抛出异常

javascript - 如何使用javascript连接azure服务总线主题

c# - log4net 在代码中配置 SqLite

c# - 从 log4net 转发到 NLog

c# - 检查DataTable中是否存在String/Record

c# - 从 C# 中的存储过程返回值

c# - 如何使用 C# 创建自签名证书?

vb.net - 如何将我的 webrole 应用程序 (vb.net) 连接到我的 sql azure 数据库?