0

私は空のWebサイトからエラーを監視してキャプチャするスクリプトを作成したいと思います。このために私は晴れのストリーミングログを利用したいですAzure Webアプリケーションのエラーログをキャプチャするためのスクリプト

これはPowershellスクリプトです。

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

Stream-Log 

上記のスクリプトを単独で実行すると、ログがストリーミングされます。

上記のスクリプトをC#クライアントから呼び出すことにしました。

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); 

    } 

なんらかの理由で、晴れのストリーミングログからの出力は、上記のいずれのイベントでもキャプチャされません。

答えて

0

あなたの説明によると、私はこのチュートリアルExecuting PowerShell scripts from C#の私の側でこの問題をテストしました。私はそれは次のように期待どおりに動作可能性があり、あなたのコードを使用して、それを修正:

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

関連する問題