English 中文(简体)
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 & 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?

最佳回答

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

问题回答

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.




相关问题
Best logging approach for composite app?

I am creating a Composite WPF (Prism) app with several different projects (Shell, modules, and so on). I am getting ready to implement logging, using Log4Net. It seems there are two ways to set up the ...

How to make logging.debug work on Appengine?

I m having a tough time getting the logging on Appengine working. the statement import logging is flagged as an unrecognized import in my PyDev Appengine project. I suspected that this was just an ...

How to validate Java logging properties files?

I have a basic facility for allowing users to remotely apply changes to the logging files in my application. Some logs are configured using java.util.logging properties files, and some are configured ...

Logging SAS scripts

I ve been developing a lot of Java, PHP and Python. All of which offer great logging packages (Log4J, Log or logging respectively). This is a great help when debugging applications. Especially if the ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

热门标签