Question

I'm using Enterprise Library 5 for logging, Is there any way to change the text formatter template to create HTML log files?

Was it helpful?

Solution

I solved the problem, by adding a formatter to my app.config and named it HTMLFormatter:

<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="                  &lt;html&gt;&#xA;                  &lt;body&gt;&#xA;&#xA;                  &lt;table border=&quot;1&quot; style=&quot;border: solid 1px #000000; border-collapse:collapse;font-family:tahoma;color:black ;font-size:12px;&quot;&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;&lt;b&gt;Message&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;{message}&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;Local TimeStamp&lt;/td&gt;&lt;td&gt;{timestamp(local)}&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;Title&lt;/td&gt;&lt;td&gt;{title}&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;Severity&lt;/td&gt;&lt;td&gt;{severity}&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;Category&lt;/td&gt;&lt;td&gt;{category}&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;Local Machine&lt;/td&gt;&lt;td&gt;{localMachine}&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;AppDomain&lt;/td&gt;&lt;td&gt;{appDomain}&lt;/td&gt;&lt;/tr&gt;&#xA;&#xA;                  &lt;tr&gt;&lt;td&gt;Extended Properties&lt;/td&gt;&lt;td&gt;&#xA;                  &lt;table border=&quot;1&quot; style=&quot;border: solid 1px #000000; border-collapse:collapse;&quot;&gt;&#xA;                  {dictionary(&lt;tr&gt;&lt;td&gt;{key}&lt;/td&gt;&lt;td&gt;{value}&lt;/td&gt;&lt;/tr&gt;)}&#xA;&#xA;                  &lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&#xA;                  &lt;/table&gt;&#xA;                  &lt;/body&gt;&#xA;                  &lt;/html&gt;&#xA;&#xA;"
name="HTMLFormatter" />

OTHER TIPS

@Masoud's approach is easy and can work in most situations (but it's possible to log invalid HTML).

Another approach would be to use a template with just the Message property set and to write a helper method that generates valid HTML (all fields escaped properly etc.).

Taking that a bit further, a custom formatter could be created to output the desired HTML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top