0

I want to create a template with XML or HTML with placeholders for exception reporting variables to be swapped in at runtime when an exception occurs. I like what I've got so far with the template, and behavior, but it's all hard coded at the moment, so bad.

Here's a sample of the code I have...

'Styling (Outlook does not follow CSS standards >:-( )
body = "<html><head>" & _
        "<style type=""text/css"">" & _
        "   * {font:12px Arial, Helvetica, sans-serif;}" & _
        "   table {border-collapse:collapse;}" & _
        "   table.grid td {padding:0 7px;border:solid #ccc 1px;}" & _
        "   .bold {font-weight:bold;padding-right:12px;}" & _
        "</style></head><body>"

'Partial template
body &= "<table><tr><td class=""bold"">Error:</td><td>" & ex.Message & "</td></tr>" & _
        "<tr><td class=""bold"">Exception:</td><td>" & ex.GetType().FullName & "</td></tr>" & _
        "<tr><td class=""bold"">Source:</td><td>" & ex.Source & "</td></tr>" & _
        "<tr><td class=""bold"">Request Url:</td><td>" & Request.Url.ToString & "</td></tr></table><br />"

Notice the ex.Message variable, etc. I want to move all the HTML code into a separate file (XML or HTML, depending on what is recommended, XML I imagine?) along with placeholders for each of the error variables. Then load the template, substitute in the variables and send the email on the fly. What is the best practice for doing this?

Also, please don't try to fix my CSS, that's not what this question is about. Outlook doesn't follow standards (go figure), and this is how I had to do it.

Thanks. ;)

Chiramisu
  • 4,687
  • 7
  • 47
  • 77

1 Answers1

1

I think you are pretty much on the right way. The steps would be:

  • Extract the templates into a resource file (the resource extension .xml, .html is the actual resource type).
  • Replace the variable concatenations with tokens; So would be: (...)<tr><td class="bold">Source:</td><td>{exSource}</td></tr>(...)
  • Create an extension method to String objects to "enhance" the String.Format, so you could do something like

    ' Load the resourceFile and put content into formattedBody
    Dim formattedBody as String = templateBody.FormatWith(New With{.exSource = ex.Source})
    

About how to implement the FormatWith, take a look here. This answer may help you as well.

Community
  • 1
  • 1
J.Hudler
  • 1,238
  • 9
  • 26
  • So how easy is this to implement using an XML template? I have almost no experience with XML. Or is XML even preferred? Should I just use HTML since this is ultimately what it needs to be formatted as in order for Outlook to read it anyway? Thanks. – Chiramisu Aug 27 '12 at 20:44
  • You don't have to use a XML object. The template would be a text file you create yourself (the .html, .xml or .txt extension wouldn't make any difference here). Just read the text file (your template), and replace your tokens with the corresponding variables you have in code. – J.Hudler Aug 27 '12 at 20:49
  • Fantastic! Took some doing, but worked like a charm. Thank you. :) – Chiramisu Aug 28 '12 at 00:02