Azure如何启用完整的WebJob日志

时间:2014-05-06 10:16:29

标签: azure azure-webjobs

当我在Windows Azure中将控制台应用程序作为WebJob运行时,在几行日志之后会添加警告:

[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

停止记录。我使用 BASIC 托管计划浏览了我网站中的所有设置,但我无法找到任何可以解决此问题的内容。

如何启用完整的webJob日志?

2 个答案:

答案 0 :(得分:20)

启用完整(连续)WebJobs日志的方法实际上在错误消息中:enable website application diagnostics,您可以通过网站的配置选项卡上的Azure门户执行此操作,您可以设置应用程序日志转到文件系统(但仅限12小时),表存储或blob存储。

启用后,WebJobs的完整日志将会显示在所选存储上。

有关Azure网站的应用程序诊断的更多信息:http://azure.microsoft.com/en-us/documentation/articles/web-sites-enable-diagnostic-log/

答案 1 :(得分:4)

您还可以使用自定义TraceWriter。

示例: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();
}

示例SqlTraceWriter实现

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