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

Masoud
- 8,020
- 12
- 62
- 123
2 Answers
1
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=" <html>
 <body>

 <table border="1" style="border: solid 1px #000000; border-collapse:collapse;font-family:tahoma;color:black ;font-size:12px;">

 <tr><td><b>Message</b></td><td><b>{message}</b></td></tr>

 <tr><td>Local TimeStamp</td><td>{timestamp(local)}</td></tr>

 <tr><td>Title</td><td>{title}</td></tr>

 <tr><td>Severity</td><td>{severity}</td></tr>

 <tr><td>Category</td><td>{category}</td></tr>

 <tr><td>Local Machine</td><td>{localMachine}</td></tr>

 <tr><td>AppDomain</td><td>{appDomain}</td></tr>

 <tr><td>Extended Properties</td><td>
 <table border="1" style="border: solid 1px #000000; border-collapse:collapse;">
 {dictionary(<tr><td>{key}</td><td>{value}</td></tr>)}

 </table></td></tr>
 </table>
 </body>
 </html>

"
name="HTMLFormatter" />

Masoud
- 8,020
- 12
- 62
- 123
-
This is the easiest approach and it can work. The downside of the approach is that the data is not guaranteed to be valid HTML so you could hit some display issues. – Randy Levy Aug 07 '13 at 18:22
0
@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.

Randy Levy
- 22,566
- 4
- 68
- 94