本文介绍了登录Azure Web作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure Web作业.我也知道TextWriter用于编写Web作业(VS 2013)的日志.但是,日志是在Blob容器下的Output logs文件夹下创建的.这些不是用户友好的.我必须打开每个文件才能阅读写入其中的消息.

I am working with Azure web jobs. Also I am aware that the TextWriter is used to write logs in case of web jobs (VS 2013). However, The logs are created under the Output logs folder under the blob container. THese are not user friendly. I have to open each file to read the message written to it.

是否有任何方法可以将记录更改为表,以方便用户阅读?

Is there any way to change the logging to table, which is user friendly to read?

谢谢.

推荐答案

您可以使用适用于Windows Azure的语义记录应用程序块.它允许您登录到Azure表存储.

You can use the Semantic Logging Application Block for Windows Azure.It allows you to log into an Azure Table Storage.

定义您的事件源:

// 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 ):

And the implementation ( implementation of your interface must be decorated with the 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);
    }
}

现在您可以像这样注册事件源:

Now you can register your event source like that :

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

这篇关于登录Azure Web作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:33