0

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?

Thanks in advance.

genericuser
  • 1,430
  • 4
  • 22
  • 40

2 Answers2

0

I'm not sure if there's a "native" way to do this, but you can add Azure Storage Client through nuget and write your own "Log To Azure Tables".

Thiago Custodio
  • 17,332
  • 6
  • 45
  • 90
  • https://msdn.microsoft.com/en-us/library/azure/gg433048.aspx This link suggests we can use diagnostic tools for the same. However I am not sure how to get to diagnostic configuration. – genericuser Aug 21 '15 at 15:12
0

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

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

[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);
Community
  • 1
  • 1
Thomas
  • 24,234
  • 6
  • 81
  • 125