Before continuing with this post, it is recommended that you should check this post where we covered the basics of .NET Tracing, Trace Listener concepts and also built a custom trace listener to write all tracing information in Azure blob storage.
In this article, we will focus on how we can integrate the custom trace listener in PowerShell.
As we know that Windows PowerShell is an automation and configuration management framework built on top of .NET framework, this means that you can easily work with all managed .NET providers in PowerShell too. You can take a look at this post for your reference where we directly consumed Azure application insights API in PowerShell.
So let’s see how we can leverage .NET tracing in PowerShell.
In previous article, we already created a class library in which we had our custom trace listener which dumps all the traced information to azure blob storage, let’s grab the output of the class library and paste in a folder where we will be creating our PowerShell script file.
Let’s create a new PowerShell script by launching Windows PowerShell ISE (you can do it using notepad file too if you are a pro PowerShell developer unlike me).
Let’s create a common function which will be responsible for writing trace information,
So let’s see how we can leverage .NET tracing in PowerShell.
In previous article, we already created a class library in which we had our custom trace listener which dumps all the traced information to azure blob storage, let’s grab the output of the class library and paste in a folder where we will be creating our PowerShell script file.
Let’s create a new PowerShell script by launching Windows PowerShell ISE (you can do it using notepad file too if you are a pro PowerShell developer unlike me).
Let’s create a common function which will be responsible for writing trace information,
- #Function to write trace information
- functionWrite-AzureTrace
- {
- param
- (
- [System.String]$logMessage,
- [ValidateSet('Show','None')]
- [System.String]$consoleOutput
- )
- [System.Diagnostics.Trace]::WriteLine($logMessage)
- }
Once we are done with creating a function, let’s make PowerShell aware that it has to use the custom trace listener which writes this trace information to blob file in Azure storage.
It is quite similar to the process of adding trace listener to any .NET based application programmatically. I.e. adding our custom trace listeners in existing listeners’ collection.
- #Importing custom trace listener dll
- Add-Type-Path"D:\Scripts\Demo.dll"
- #Initializing required attrbutes of trace listener
- $customListener=new-object"Demo.BlobWriterStorageListener"
- $customListener.Attributes.Add("StorageConnectionString","Your_storage_conn_string")
- $customListener.Attributes.Add("LogsContainerName","logs")
- $customListener.Attributes.Add("LogFileName","application.log")
- #Adding custom trace listener to collection
- [System.Diagnostics.Trace]::Listeners.Add($customListener)
As a last step, we are registering object of custom trace listener with existing trace listeners’ collection.
By doing this we are all done with setting required configurations, so let’s go ahead and use the function.
- Write-AzureTrace-logMessage"Hey There! This is from PowerShell!"-consoleOutputShow

Let’s open up this file and see the logged entry.

Isn’t it cool?
Below is the entire source.
- #Importing custom trace listener dll
- Add-Type-Path"D:\Scripts\Demo.dll"
- #Initializing required attrbutes of trace listener
- $customListener=new-object"Demo.BlobWriterStorageListener"
- $customListener.Attributes.Add("StorageConnectionString","Your_storage_conn_string")
- $customListener.Attributes.Add("LogsContainerName","logs")
- $customListener.Attributes.Add("LogFileName","application.log")
- #Adding custom trace listener to collection
- [System.Diagnostics.Trace]::Listeners.Add($customListener)
- #Function to write trace information
- FunctionWrite-AzureTrace
- {
- param
- (
- [System.String]$logMessage,
- [ValidateSet('Show','None')]
- [System.String]$consoleOutput
- )
- [System.Diagnostics.Trace]::WriteLine($logMessage)
- }
- #Writing Trace
- Write-AzureTrace-logMessage"Hey There! This is from PowerShell!"-consoleOutputShow

Munesh SharmaPosted May 22, 2016, 10:54 AM
good one
Kuppurasu NagarajPosted May 22, 2016, 5:19 AM
Nice Sharing..
Debasis SahaPosted May 20, 2016, 10:49 AM
Nice One..
Thiruppathi RPosted May 20, 2016, 1:23 AM
Good..
Bhavik PatelPosted May 20, 2016, 12:35 AM
nice
Prasanna MuraliPosted May 20, 2016, 12:34 AM
Nice one....
Prasham SabadraPosted May 19, 2016, 2:19 PM
Thanks Bhushan for sharing!!!