Robert MacLean
Robert MacLean

Reputation: 39261

Have log entries forced to a single line when using p&p EntLib logging block

I am using the patterns & practices enterprise library (p&p EntLib) logging block to write to a text file.

Some of the log message contain multiple lines which I would like to force to become a single line - in other words replacing the \r & \n with a space in the log messages prior to them being written to the file.

How do I configure the logging block for this, if this is possible?

Upvotes: 0

Views: 1361

Answers (2)

Kane
Kane

Reputation: 16802

From memory you will need to create a custom formatter, something similar to this link.

Upvotes: 1

Randy Levy
Randy Levy

Reputation: 22655

What about performing a string replace on your message to substitute spaces for newlines?

i.e.

Configure your Formatter template something like:

<formatters>
  <add template="{timestamp}: {category}: {severity}: {message}"
    type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="My Text Formatter" />
</formatters>


Then in your code replace the new lines with a space (this could be done in one centralized helper method):

string message = @"This is 
the message
to log.";

message.Replace(Environment.NewLine, " ");


This should result in a log file that looks like:

11/11/2009 2:52:01 PM: My Category Event: Information: This is  the message to log.
11/11/2009 2:53:47 PM: My Category Event: Information: More test to log to the  file.

Upvotes: 0

Related Questions