سؤال

When I run a console application as a WebJob in Windows Azure, after few lines of log it adds a warning:

[05/06/2014 09:42:40 > 21026c: WARN] Reached maximum allowed output lines for this run, to see all of the job's logs you can enable website application diagnostics

And stops logging. I was going through all settings in my Website with BASIC hosting plan but I was unable to find anything that would fix this issue.

How can I enable full webJob logs?

هل كانت مفيدة؟

المحلول

The way to enable full (continuous) WebJobs logs is actually in the error message: enable website application diagnostics, you can do this via the Azure portal on the website's CONFIGURE tab, you can set the application logs to go to either the file-system (but only for 12 hours), table storage or blob storage.

Once enabled the full logs for WebJobs will go on the selected storage.

More info on application diagnostics for Azure websites: http://azure.microsoft.com/en-us/documentation/articles/web-sites-enable-diagnostic-log/

نصائح أخرى

You could also potentially use a custom TraceWriter.

Example: https://gist.github.com/aaronhoffman/3e319cf519eb8bf76c8f3e4fa6f1b4ae

JobHost config

static void Main()
{
    var config = new JobHostConfiguration();

    // Log Console.Out to SQL using custom TraceWriter
    // Note: Need to update default Microsoft.Azure.WebJobs package for config.Tracing.Tracers to be exposed/available
    config.Tracing.Tracers.Add(new SqlTraceWriter(
        TraceLevel.Info,
        "{{SqlConnectionString}}",
        "{{LogTableName}}"));

    var host = new JobHost(config);
    host.RunAndBlock();
}

Sample SqlTraceWriter implementation

public class SqlTraceWriter : TraceWriter
{
    private string SqlConnectionString { get; set; }

    private string LogTableName { get; set; }

    public SqlTraceWriter(TraceLevel level, string sqlConnectionString, string logTableName)
        : base(level)
    {
        this.SqlConnectionString = sqlConnectionString;
        this.LogTableName = logTableName;
    }

    public override void Trace(TraceEvent traceEvent)
    {
        using (var sqlConnection = this.CreateConnection())
        {
            sqlConnection.Open();

            using (var cmd = new SqlCommand(string.Format("insert into {0} ([Source], [Timestamp], [Level], [Message], [Exception], [Properties]) values (@Source, @Timestamp, @Level, @Message, @Exception, @Properties)", this.LogTableName), sqlConnection))
            {
                cmd.Parameters.AddWithValue("Source", traceEvent.Source ?? "");
                cmd.Parameters.AddWithValue("Timestamp", traceEvent.Timestamp);
                cmd.Parameters.AddWithValue("Level", traceEvent.Level.ToString());
                cmd.Parameters.AddWithValue("Message", traceEvent.Message ?? "");
                cmd.Parameters.AddWithValue("Exception", traceEvent.Exception?.ToString() ?? "");
                cmd.Parameters.AddWithValue("Properties", string.Join("; ", traceEvent.Properties.Select(x => x.Key + ", " + x.Value?.ToString()).ToList()) ?? "");

                cmd.ExecuteNonQuery();
            }
        }
    }

    private SqlConnection CreateConnection()
    {
        return new SqlConnection(this.SqlConnectionString);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top