我已经设置了hangfire我的 ASP.NET 项目成功,即在我的数据库中创建了 11 个 Hangfire 表。我在里面尝试了以下命令Application_Start()
我的项目的Global.asax
:
namespace myAPI
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start(
{
System.Diagnostics.Debug.WriteLine("Recurring job will be set up.");
RecurringJob.AddOrUpdate(
"some-id",
() => System.Diagnostics.Debug.WriteLine("Job instance started at " +
DateTime.Now)),
"*/2 * * * 1-5");
}
}
}
遗憾的是,在 Visual Studio 的窗口内输出>调试我只看到Reccuring job will be set up.
从此以后什么也没有了。然而,一个SELECT * FROM [myContext].[HangFire].[Set]
给我看
Key Score Value ExpireAt
recurring-jobs 1579116240 some-id NULL
到目前为止一切顺利,这意味着这项工作确实已经完成。
But 我如何登录在我的数据库中每次执行 RecurringJob 时?我是否正确地假设 Hangfire 不会开箱即用,我必须自己在箭头功能中记录它?或者有更优雅的方式吗?
侧面提问:为什么我看不到任何输出System.Diagnostics.Debug.WriteLine
within我的经常性工作?
参考
- Hangfire 不会在 IIS 中创建表
- 如何使用 ASP.NET 配置hangfire 以从配置文件中获取连接字符串?
- 关于经常性任务的官方hangfire.io文档
您可以使用SeriLog 与 Hangfire盒子外面。 Serilog 配有不同的接收器,例如Serilog.Sinks.MSSqlServer
。您可以将其配置在startup.cs
:
using Serilog;
using Serilog.Sinks.MSSqlServer;
Log.Logger = new LoggerConfiguration()
.WriteTo
.MSSqlServer(
connectionString: hangfireConnectionString,
tableName: "Logs",
autoCreateSqlTable: true
).CreateLogger();
// will display any issues with Serilog config. comment out in prod.
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
GlobalConfiguration.Configuration
.UseSqlServerStorage(hangfireConnectionString)
.UseSerilogLogProvider();
安排工作后,您可以使用以下方式记录它
Log.Information(string.Format("Hanfire Job Scheduled at {0}", DateTime.Now));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)