How do I write to the logs in Azure WebJobs from a C# console app?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 01:00:16

问题


I'm testing out Azure Webjobs. I wrote a console application which polls a SQL database for new work and processes it. I'm not using the WebJobs SDK because it only supports Azure Storage.

I upload the job, it runs, and then it fails with a exception that says it wasn't able to connect to the SQL Database instance. I'm wondering what connection string is being used; is it getting the connection string from the Azure Website. The logs give me this:

[03/14/2014 22:24:25 > 512206: SYS INFO] Status changed to Running
[03/14/2014 22:24:40 > 512206: ERR ] 
[03/14/2014 22:24:40 > 512206: ERR ] Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I would like to write data to these logs (like what is the connection string that is being used). I tried Console.WriteLine, Debug.WriteLine, Console.Error.WriteLine. None of them show up in the in my WebJob log.

Apparently I could get the data by just raising an exception with the message text showing what I want, but there must be a better way! How do I write SYS INFO lines and ERR lines to the log?


回答1:


Regarding logs:

For continuous WebJobs - Console.Out and Console.Error are routed to the "application logs", they will show up as file, blob or table storage depends on your configuration of the application logs (similar to your Website).

Also the first 100 lines in each invocation will also go to the WebJob log file (to ease debugging pain when the WebJob fails at startup) accessible using the Azure portal (but also saved on your site's file system at data/jobs/continuous/jobName).

For triggered/scheduled WebJobs - Console.Out/Console.Error are routed to the WebJobs specific run log file also accessible using the Azure portal and stored at data/jobs/triggered/jobName/runId.

Console.Out is treated (marked) as INFO and Console.Error as ERROR.

Regarding connection strings:

You can access your connection strings the same as in your website, using the ConfigurationManager class, for WebJobs not written in .NET you can find these connection strings (and app settings) as environment variables (all the same as with your Website).




回答2:


I was able to get Console.WriteLine() to leave messages in my web job log. The following console runs and leaves log messages.

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            DoStuff();
            Thread.Sleep(10000);
        }
    }

    public static void DoStuff()
    {
        Console.WriteLine("do stuff");
    }
}

This is what my log file shows:

[03/15/2014 04:05:28 > cf6d00: SYS INFO] Run script 'HelloWebJobConsoleApplication.exe' with script host - 'WindowsScriptHost'
[03/15/2014 04:05:28 > cf6d00: SYS INFO] Status changed to Running
[03/15/2014 04:05:28 > cf6d00: INFO] do stuff
[03/15/2014 04:05:38 > cf6d00: INFO] do stuff
[03/15/2014 04:05:48 > cf6d00: INFO] do stuff
[03/15/2014 04:05:58 > cf6d00: INFO] do stuff


来源:https://stackoverflow.com/questions/22417081/how-do-i-write-to-the-logs-in-azure-webjobs-from-a-c-sharp-console-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!