Add tracelistener with powershell

In our deployment script we automate the installation and configuration of our product. For Development and Test environment we want to log everything to a file. The powershell script below takes care of adding TextWriterTraceListener to the trace listeners.

$configFile = "YOUR_CONFIG_FILE"
$xml=New-Object XML
# locate the sharedlistener with name TextFileLogListener
$tracelistenernode = $xml.SelectSingleNode("/configuration/system.diagnostics/sharedListeners/add[@name='TextFileLogListener']")
if (!$tracelistenernode)
    # filter on eventtype Information
    $filter = $xml.CreateElement("filter")
    $filter.SetAttribute("type", "System.Diagnostics.EventTypeFilter")
    $filter.SetAttribute("initializeData", "Information")
    # TextWriterTraceListener in shared listeners
    $tracelistenernode = $xml.CreateElement("add")
    $tracelistenernode.SetAttribute("name", "TextFileLogListener")
    $tracelistenernode.SetAttribute("type", "System.Diagnostics.TextWriterTraceListener")
    $tracelistenernode.SetAttribute("initializeData", "c:\\log.txt")
    # TextFileLogListener in tracelistenerd
    $listener = $xml.CreateElement("add")
    $listener.SetAttribute("name", "TextFileLogListener")
    # write the config changes to disk

The filter is set to Information. This means everything is logged and you can end up with a huge file. Feel free to use the script, but you do this on your own risk.

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.