简体   繁体   English

登录Azure Web作业

[英]Logging in Azure web jobs

I am working with Azure web jobs. 我正在使用Azure Web作业。 Also I am aware that the TextWriter is used to write logs in case of web jobs (VS 2013). 我也知道TextWriter用于编写Web作业(VS 2013)的日志。 However, The logs are created under the Output logs folder under the blob container. 但是,日志是在Blob容器下的Output logs文件夹下创建的。 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? 有什么方法可以将记录更改为表,该表易于用户读取?

Thanks in advance. 提前致谢。

我不确定是否有“本机”方法来执行此操作,但是您可以通过nuget添加Azure存储客户端并编写自己的“登录到Azure表”。

You can use the Semantic Logging Application Block for Windows Azure. 您可以使用Windows Azure的语义记录应用程序块 It allows you to log into an Azure Table Storage. 它允许您登录到Azure表存储。

Define your 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);
}

Implement the interface : 实现接口:

And the implementation ( implementation of your interface must be decorated with the NonEventAttribute see this post ) : 和实现(您的接口的实现必须用NonEventAttribute装饰), 请参见

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM