cod*_*nja 10 c# asp.net quartz-scheduler quartz.net
我有一个Quartz作业,Application_Start在此期间设置Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
Logger.log("About to Setup Retry Job");
JobScheduler.Start();
}
Run Code Online (Sandbox Code Playgroud)
这会调用该Start方法,然后调度作业.
作业每20秒运行一次并抛出异常.这是我的工作.
public class RetryTempJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
try
{
Logger.log("Executing Job");
new ProcessOrder().retryFailedOrders();
//Logger.log("Done Executing Syspro Job");
await Console.Error.WriteLineAsync("Done Executing Syspro Job");
}
catch (Exception se)
{
await Console.Error.WriteLineAsync("" + se.InnerException);
}
}
}
Run Code Online (Sandbox Code Playgroud)
此行引发异常Logger.log("Executing Job");.这是一个静态方法,它打开一个日志文件并写入它.此方法适用于我的网站中的其他位置.
以下是异常消息:{"对象引用未设置为对象的实例."}
InnerException为NULL.这是堆栈:
DarwinsShoppingCart.dll!DarwinsShoppingCart.SharedClasses.JobScheduler.RetrySyspro.Execute(Quartz.IJobExecutionContext context) Line 69 C#
Quartz.dll!Quartz.Core.JobRunShell.Run(System.Threading.CancellationToken cancellationToken) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Quartz.Core.JobRunShell.<Run>d__9>(ref Quartz.Core.JobRunShell.<Run>d__9 stateMachine) Unknown
Quartz.dll!Quartz.Core.JobRunShell.Run(System.Threading.CancellationToken cancellationToken) Unknown
Quartz.dll!Quartz.Core.QuartzSchedulerThread.Run.AnonymousMethod__0() Unknown
mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.Task>.InnerInvoke() Unknown
mscorlib.dll!System.Threading.Tasks.Task.Execute() Unknown
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Unknown
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Unknown
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Unknown
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Unknown
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Unknown
Run Code Online (Sandbox Code Playgroud)
这是我的Logger类代码
public static void log(string strLog)
{
StreamWriter log;
FileStream fileStream = null;
DirectoryInfo logDirInfo = null;
FileInfo logFileInfo;
string username = Environment.UserName;
string logFilePath = HttpContext.Current.Server.MapPath("~/log/Log.txt");
logFileInfo = new FileInfo(logFilePath);
logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
double fileSize = ConvertBytesToMegabytes(logFileInfo.Length);
if (fileSize > 30)
{
string FileDate = DateTime.Now.ToString().Replace("/", "-").Replace(":", "-");
string oldfilepath = HttpContext.Current.Server.MapPath("~/log/log-" + FileDate + ".txt");
File.Move(logFileInfo.FullName, oldfilepath);
}
if (!logFileInfo.Exists)
{
fileStream = logFileInfo.Create();
}
else
{
fileStream = new FileStream(logFilePath, FileMode.Append);
}
log = new StreamWriter(fileStream);
log.WriteLine(DateTime.Now.ToString("MM-dd HH:mm:ss") + " " + username + " " + strLog);
log.Close();
}
Run Code Online (Sandbox Code Playgroud)
HttpContext使用 Quarz Job 时没有。它在单独的线程中运行。所以在我使用的带有 Quarz Job 的网站上HostingEnvironment
所以代替
HttpContext.Current.Server.MapPath("~/log/Log.txt")
Run Code Online (Sandbox Code Playgroud)
使用
using System.Web.Hosting;
HostingEnvironment.MapPath("~/log/Log.txt");
Run Code Online (Sandbox Code Playgroud)