【问题标题】:Intercepting log messages in nlog在 nlog 中拦截日志消息
【发布时间】:2018-07-29 04:32:32
【问题描述】:

我们需要拦截 nlog 正在写入磁盘的条目,以便可以对任何 URL/IP 地址和类似的安全敏感信息进行混淆或以某种方式进行编码。

例如,如果记录了任何 URL,则应将其从 http://servername:2212/webservice 至 (http)://servername/2212/webservice

是否有任何 nlog 模式可以采用正则表达式并转换为另一个字符串?

或者是否可以通过截取日志文件的写入来实现?

任何帮助、建议或重定向都会非常有帮助。

我们正在使用 .NET 4.5、nlog 4.4、C#

【问题讨论】:

    标签: c# .net logging nlog


    【解决方案1】:

    写一个包装器layoutrenderer,可以包装比所有的布局渲染器。 .例如如果你有布局${message}${exception},它将是

    ${intercept:inner=${message}${exception}} 
    

    代码:

    [LayoutRenderer("intercept")]
    public sealed class InterceptLayoutRendererWrapper : WrapperLayoutRendererBase
    {
    
        /// <summary>
        /// Option you could set from config or code
        /// </summary>
        [DefaultValue(true)]
        public bool Option1 { get; set; }
    
    
        /// <summary>
        /// Renders the inner layout contents.
    
        /// OR override TransformFormattedMesssage, and change the stringbuilder (less string allocations)
        /// </summary>
        /// <param name="logEvent">The log event.</param>
        /// <returns>Contents of inner layout.</returns>
        protected override string RenderInner(LogEventInfo logEvent)
        {
            var text = this.Inner.Render(logEvent);
            return interceptSomething(text);
        }
    
    }
    

    并注册:

    LayoutRenderer.Register<InterceptLayoutRendererWrapper>("intercept"); 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多