Search code examples
azureazure-powershellazure-web-app-serviceazure-diagnostics

script for capturing Azure web application error logs


I would like to write a script which monitors and captures errors from a azure web site. For this i would like to utilize azure streaming logs

Powershell script for this.

function Stream-Log
{
 Get-AzureWebsiteLog -Name HiWebApiService -Tail
 }

 Stream-Log

if i alone execute above script it is streaming the logs.

I wanted to invoke above script from a c# client.

 class Program
{
    static void Main(string[] args)
    {
        PowerShell psinstance = PowerShell.Create();
        const string getverbose = "$verbosepreference='continue'";
        psinstance.AddScript(string.Format(getverbose));
        psinstance.Invoke();
        psinstance.Commands.Clear();

        var scriptPath = @"E:\Azure\LogMonitor\LogMonitor\LogMonitor.ps1";

        psinstance.AddScript(scriptPath);
        psinstance.Streams.Verbose.DataAdded += Verbose_DataAdded;
        psinstance.Streams.Information.DataAdded += Information_DataAdded;
        psinstance.Streams.Error.DataAdded += Error_DataAdded;
        var results = psinstance.Invoke();


        Console.ReadLine();
    }

    private static void Information_DataAdded(object sender, DataAddedEventArgs e)
    {
        var newRecord = ((PSDataCollection<InformationRecord>)sender)[e.Index];
        Console.WriteLine("information updated: {0}", newRecord.Source);
    }

    private static void Verbose_DataAdded(object sender, DataAddedEventArgs e)
    {
        var coll = (PSDataCollection<VerboseRecord>)sender;
        var newRecord = (coll)[e.Index];
        Console.WriteLine("verbose updated: {0}", newRecord.Message);
    }

    private static void Error_DataAdded(object sender, DataAddedEventArgs e)
    {
        ErrorRecord newRecord = ((PSDataCollection<ErrorRecord>)sender)[e.Index];
        Console.WriteLine("error updated: {0}", newRecord.ErrorDetails);

    }

For some reason the output from azure streaming logs aren't captured in any of above events.


Solution

  • According to your description, I followed this tutorial Executing PowerShell scripts from C# for testing this issue on my side. I used your code and modified it, then it could work as expected as follows:

    class Program
    {
        static void Main(string[] args)
        {
            PowerShell psinstance = PowerShell.Create();
    
            psinstance.AddScript("Get-AzureWebsiteLog -Name brucewebapp -Tail");
    
            // prepare a new collection to store output stream objects
            PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
            outputCollection.DataAdded += (s,e)=> {
                var newRecord = ((PSDataCollection<PSObject>)s)[e.Index];
                Console.WriteLine(newRecord);
            };
    
            psinstance.Streams.Verbose.DataAdded += Verbose_DataAdded;
            psinstance.Streams.Information.DataAdded += Information_DataAdded;
            psinstance.Streams.Error.DataAdded += Error_DataAdded;
            psinstance.Invoke(null, outputCollection);
    
            Console.ReadLine();
        }
    
        private static void Information_DataAdded(object sender, DataAddedEventArgs e)
        {
            var newRecord = ((PSDataCollection<InformationRecord>)sender)[e.Index];
            Console.WriteLine("information updated: {0}", newRecord.Source);
        }
    
        private static void Verbose_DataAdded(object sender, DataAddedEventArgs e)
        {
            var coll = (PSDataCollection<VerboseRecord>)sender;
            var newRecord = (coll)[e.Index];
            Console.WriteLine("verbose updated: {0}", newRecord.Message);
        }
    
        private static void Error_DataAdded(object sender, DataAddedEventArgs e)
        {
            ErrorRecord errorRecord = ((PSDataCollection<ErrorRecord>)sender)[e.Index];
            Console.WriteLine("error updated: {0}", errorRecord.Exception.Message);
    
        }
    }
    

    enter image description here