Logging Output of Azure Startup Tasks to the Event Log
Posted
by Your DisplayName here!
on Least Privilege
See other posts from Least Privilege
or by Your DisplayName here!
Published on Fri, 04 Mar 2011 09:24:44 GMT
Indexed on
2011/03/04
15:32 UTC
Read the original article
Hit count: 225
Azure
This can come in handy when troubleshooting:
using System;
using System.Diagnostics;
using System.Text;
namespace Thinktecture.Azure
{
class Program
{
static EventLog _eventLog = new EventLog("Application", ".", "StartupTaskShell");
static StringBuilder _out = new StringBuilder(64);
static StringBuilder _err = new StringBuilder(64);
static int Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Invalid arguments: " + String.Join(", ", args));
_eventLog.WriteEntry("Invalid arguments: " + String.Join(", ", args));
return -1;
}
var task = args[0];
ProcessStartInfo info = new ProcessStartInfo()
{
FileName = task,
WorkingDirectory = Environment.CurrentDirectory,
UseShellExecute = false,
ErrorDialog = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var process = new Process();
process.StartInfo = info;
process.OutputDataReceived += (s, e) =>
{
if (e.Data != null)
{
_out.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (s, e) =>
{
if (e.Data != null)
{
_err.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
var outString = _out.ToString();
var errString = _err.ToString();
if (!string.IsNullOrWhiteSpace(outString))
{
outString = String.Format("Standard Out for {0}\n\n{1}", task, outString);
_eventLog.WriteEntry(outString, EventLogEntryType.Information);
}
if (!string.IsNullOrWhiteSpace(errString))
{
errString = String.Format("Standard Err for {0}\n\n{1}", task, errString);
_eventLog.WriteEntry(errString, EventLogEntryType.Error);
}
return 0;
}
}
}You then wrap your startup tasks with the StartupTaskShell and you’ll be able to see stdout and stderr in the application event log.
© Least Privilege or respective owner