26

当我在 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 日志?

4

2 回答 2

25

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

启用后,WebJobs 的完整日志将保存在选定的存储上。

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

于 2014-05-06T16:39:23.730 回答
4

您还可以使用自定义 TraceWriter。

示例:https ://gist.github.com/aaronhoffman/3e319cf519eb8bf76c8f3e4fa6f1b4ae

JobHost配置

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);
    }
}
于 2016-07-27T16:44:55.253 回答