Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 753
Geoff Armstrong Avatar asked Mar 14 '14 22:03

Geoff Armstrong


People also ask

How do I check Azure WebJobs logs?

Click on the WebJob that you want to monitor and then click on logs: This will open a new window and you will need to login with the same credentials that you use on the Azure Portal. You will be able to monitor the WebJobs from the dashboard.

What is the difference between Azure functions and WebJobs?

Summary. Azure Functions offers more developer productivity than Azure App Service WebJobs does. It also offers more options for programming languages, development environments, Azure service integration, and pricing. For most scenarios, it's the best choice.


1 Answers

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

like image 139
Amit Apple Avatar answered Oct 06 '22 23:10

Amit Apple