0

I want to sanitize some sensitive information, like "xauth_password" from both URL and HTTP headers, so I managed to write customized filter below:

public class SanitizeLogContentFilter : FilterSkeleton
{
    public override FilterDecision Decide(LoggingEvent loggingEvent)
    {
        if (loggingEvent.RenderedMessage.IndexOf("x_auth_password", StringComparison.InvariantCultureIgnoreCase) != -1)
        {
            // Sanitize XAuth password from URL
            var sensitiveMsg = Regex.Replace(loggingEvent.RenderedMessage, "x_auth_password.*?&", "x_auth_password=******");
            // Sanitize XAuth password from HTTP Header
            sensitiveMsg = Regex.Replace(sensitiveMsg, "x_auth_password.*?\n", "x_auth_password: ******");
            using (TextWriter textWriter = new StringWriter(new StringBuilder(sensitiveMsg)))
            {
                loggingEvent.WriteRenderedMessage(textWriter);
            }

            return FilterDecision.Neutral;
        }

        return FilterDecision.Neutral;
    }
}

I know that "loggingEvent.WriteRenderedMessage" is meaning to write the current log entry to a stream, it will NOT modify the content, and "loggingEvent.RenderedMessage" is a readonly propery.

Then is there a way to modify the content before log4net flush the log entry into disk file?

Wayne Ye
  • 2,464
  • 3
  • 25
  • 29
  • @sgmoore Not duplicate at all! That thread was talking about implementing a customized PatternLayoutConverter, so that app can modify the pattern or add extra information, whereas I want to "remove" content from the log entry. – Wayne Ye Dec 18 '13 at 05:32
  • Still looks like a duplicate to me. The question was about removing passwords. The answer was to implement a PatternLayoutConverter to remove the password. – sgmoore Dec 18 '13 at 11:28
  • @sgmoore Sorry! You are right, it is relatively duplicate:) I resolved this problem by replacing the log4net %message with my own %FOO, then I have whole control of my %FOO message during runtime. Thank you! – Wayne Ye Dec 18 '13 at 12:45

0 Answers0