【问题标题】:Can we set the characters limited to 10 characters in log4net %property?我们可以在 log4net %property 中设置字符限制为 10 个字符吗?
【发布时间】:2018-10-18 02:18:20
【问题描述】:

这是我的 threadContext 异常消息属性

log4net.ThreadContext.Properties["excmessage"] = ex.Message;

我想使用 log4net 获取异常消息属性的前 10 个字符。

这是 Log4net.config 中的行:

%property{excmessage}

【问题讨论】:

  • 不确定是否可以在 log4net 配置中执行此操作,但您可以执行 log4net.ThreadContext.Properties["excmessage"] = ex.Message.Substring(0,Math.Min(ex.Message.Length,10)); 除非您在其他地方记录了完整的错误消息,否则不建议这样做。
  • @sgmoore 我的日志中有全文消息,但我希望它更短,以便在电子邮件中显示。我希望它使用 log4net。

标签: exception logging properties log4net threadcontext


【解决方案1】:

据我所知,你只能“从头截断”,这意味着你得到了字符串的结尾:

%.10property{excmessage}

这里是文档的链接: http://logging.apache.org/log4net/log4net-1.2.11/release/sdk/log4net.Layout.PatternLayout.html

我建议您填写两个属性:一个带有异常消息,另一个带有截断消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2012-02-27
    • 2010-12-24
    • 2022-06-10
    • 2011-05-15
    相关资源
    最近更新 更多