如何从C#控制台应用程序写入Azure WebJobs中的日志? [英] How do I write to the logs in Azure WebJobs from a C# console app?

查看:69
本文介绍了如何从C#控制台应用程序写入Azure WebJobs中的日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试Azure Webjobs.我编写了一个控制台应用程序,该应用程序轮询SQL数据库进行新工作并对其进行处理.我不使用WebJobs SDK,因为它仅支持Azure存储.

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.

我上传了作业,它运行了,然后失败了,但有一个例外,说它无法连接到SQL数据库实例.我想知道正在使用什么连接字符串;是从Azure网站获取连接字符串吗?日志给了我这个:

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)

我想将数据写入这些日志(例如正在使用的连接字符串是什么).我尝试了Console.WriteLine,Debug.WriteLine,Console.Error.WriteLine.它们都没有显示在我的WebJob日志中.

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.

显然,我可以通过引发一个异常并显示我想要的消息文本来获取数据,但是必须有更好的方法!如何将SYS INFO行和ERR行写入日志?

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?

推荐答案

关于日志:

对于连续 Web作业-Console.Out和Console.Error路由到应用程序日志",它们将显示为文件,blob或表存储,具体取决于您对应用程序日志的配置(类似于您的网站).

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

此外,每次调用的前100行也将转到WebJob日志文件(以减轻启动时WebJob失败时的调试痛苦),该文件可通过Azure门户访问(但也保存在站点的文件系统中的data/jobs/中)连续的/职位名称).

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

对于已触发/已计划 WebJobs-Console.Out/Console.Error被路由到特定于WebJobs的运行日志文件,该文件也可以使用Azure门户进行访问,并存储在data/jobs/triggered/jobName/runId中

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被视为(标记为)INFO,而Console.Error被视为ERROR.

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

关于连接字符串:

您可以使用ConfigurationManager类访问与网站中相同的连接字符串,对于不是用.NET编写的WebJob,您可以找到这些连接字符串(和应用程序设置)作为环境变量(与您的网站相同) ).

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

这篇关于如何从C#控制台应用程序写入Azure WebJobs中的日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆