开发者

Override Log.Write in Enterprise Library

开发者 https://www.devze.com 2023-01-13 20:16 出处:网络
What I am trying to do is pass multiple parameters to Log.Write without first creating a long string of them with newline escape characters.

What I am trying to do is pass multiple parameters to Log.Write without first creating a long string of them with newline escape characters.

Here is my current function:

public static void LogPipedEvent(string message)
    {
        //Split message on double pipe escape "||"
        string newmessage = "";
        string[] splitMessage = Regex.Split(message, Regex.Escape("||"));

        foreach (string line in splitMessage)
        {
            newmessage += "\r\n" + line;
        }
        Logger.Write(newmessage, "DebugCategory", 2, 4000, TraceEventType.Information, "Message");
    }

It works fine for what I need it to do but I am wondering if there is a cleaner way to break up t开发者_Go百科he "message" portion of the log and have each entry get it's own line within Enterprise library?


If you know that your delimiter is || and you simply need to replace it with new lines, you can use string.Replace:

public static void LogPipedEvent(string message)
{
    Logger.Write(message.Replace("||", "\r\n"), "DebugCategory", 2, 4000, TraceEventType.Information, "Message");
}


If you have a collection of information that you want to log then you could consider using the LogEntry's ExendedProperties. The ExtendedProperties is a Dictionary of key/value pairs.

So instead of passing in a pipe delimited string, you could pass in a string and a Dictionary and use a Formatter to output the message in your desired format (out key/value can be on it's own line).

I'm not sure if that fits with the type of data you are dealing with but that is an "Enterprise Library-ish" way to do it.


Can you change your signature? If you did this:

public static void LogMessage(params string[] messages)
{
    string message = messages.Join("\r\n");
    Logger.Write(newmessage, "DebugCategory", 2, 4000, TraceEventType.Information, "Message"););
}

Then you could call it like this:

LogMessage("Message 1", "Message 2", "Message 3");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号