登录Azure Web作业

时间:2015-08-20 18:31:20

标签: azure logging azure-webjobs

我正在使用Azure网络作业。另外我知道TextWriter用于在Web作业的情况下写入日志(VS 2013)。但是,日志是在blob容器下的Output logs文件夹下创建的。这不是用户友好的。我必须打开每个文件才能读取写入它的消息。

有没有办法将日志记录更改为用户友好的日志?

提前致谢。

2 个答案:

答案 0 :(得分:0)

我不确定是否有“原生”方法可以执行此操作,但您可以通过nuget添加Azure Storage Client并编写自己的“Log To Azure Tables”。

答案 1 :(得分:0)

您可以将Semantic Logging Application Block用于Windows Azure。 它允许您登录Azure表存储。

定义您的Eventsource:

// A simple interface to log what you need ...
public interface ILog
{
    void Debug(string message);

    void Info(string message);

    void Warn(string message);

    void Error(string message);

    void Error(string message, Exception exception);
}

实施界面:

实施(界面的实施必须使用NonEventAttribute see this post进行修饰):

[EventSource(Name = "MyLogEventsource")]
public class Log : EventSource, ILog
{
    public Log()
    {
        EventSourceAnalyzer.InspectAll(this);
    }

    [NonEvent]
    public void Debug(string message)
    {
        DebugInternal(message);
    }

    [Event(1)]
    private void DebugInternal(string message)
    {
        WriteEvent(1, message);
    }

    [NonEvent]
    public void Info(string message)
    {
        InfoInternal(message);
    }

    [Event(2)]
    private void InfoInternal(string message)
    {
        WriteEvent(2, message);
    }

    [NonEvent]
    public void Warn(string message)
    {
        WarnInternal(message);
    }

    [Event(3)]
    private void WarnInternal(string message)
    {
        WriteEvent(3, message);
    }

    [NonEvent]
    public void Error(string message)
    {
        ErrorInternal(message, "", "");
    }

    [NonEvent]
    public void Error(string message, Exception exception)
    {
        ErrorInternal(message, exception.Message, exception.ToString());
    }

    [Event(4)]
    private void ErrorInternal(string message, string exceptionMessage, string exceptionDetails)
    {
        WriteEvent(4, message, exceptionMessage, exceptionDetails);
    }
}

现在您可以注册您的事件来源:

var log = new Log();
var eventListeners = new List<ObservableEventListener>();
// Log to Azure Table
var azureListener = new ObservableEventListener();
azureListener.EnableEvents(log , EventLevel.LogAlways, Keywords.All);
azureListener.LogToWindowsAzureTable(
            instanceName: Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") ?? "DevelopmentInstance",
            connectionString: CloudConfigurationManager.GetSetting("MyStorageConnectionString")
            tableAddress: "MyLogTable");
eventListeners .Add(azureListener);