Hangfire 入門 (使用MySQL存儲)


 最近公司的項目要求每小時計算數據並生成報告,使用的Hangfire來實現。

之前對Hangfire並不了解,所以學習並記錄下來,希望對大家也有幫助。

環境:

.NET Framework 4.7.2

Hangfire.Core 1.7.12

Hangfire.AspNet 0.2.0

Hangfire.MySqlStorage 2.0.3

 

這篇文章主要根據官網的內網做一個基礎入門,實現最基礎的功能。

搭建基礎框架

首先,我創建了一個Web API項目,然后添加Startup類(因為我要使用Hangfire的Dashboard,所以使用Startup類進行配置)。

 

 

 

因為我使用的是MySQL數據庫(使用SQL Server可以去看官網的例子),使用NuGet添加:

Install-Package Hangfire.MySqlStorage

可以去GitHub查看這個包的說明:Hangfire MySql Storage

 將官網的實例代碼復制到Startup類中,並修改了數據存儲的配置如下:

public class Startup
{
    private IEnumerable<IDisposable> GetHangfireServers()
    {
        GlobalConfiguration.Configuration
            .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseStorage(new MySqlStorage("server=127.0.0.1;user id=root;password=111111;Database=hangfiretest;pooling=true;charset=utf8;Allow User Variables=True;", new MySqlStorageOptions
            {
                    //CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    //SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    //QueuePollInterval = TimeSpan.Zero,
                    //UseRecommendedIsolationLevel = true,
                    //DisableGlobalLocks = true
            }));

        yield return new BackgroundJobServer();
    }

    public void Configuration(IAppBuilder app)
    {
        app.UseHangfireAspNet(GetHangfireServers);
        app.UseHangfireDashboard();

        // Let's also create a sample background job
        BackgroundJob.Enqueue(() => Debug.WriteLine("Hello world from Hangfire!"));

        // ...other configuration logic
    }
}

說明:

  • Github上面也提示我們連接字符串要設置允許用戶變量,不然你就要收到一個異常了。

    There must be Allow User Variables set to true in the connection string. For example: 

    server=127.0.0.1;uid=root;pwd=root;database={0};Allow User Variables=True

    

  •  MySQL的MySqlStorageOptions選項和SQLServer不同,這里也不清楚這些配置的作用,先全部注釋掉了,以后再研究。

 

這時候就可以運行了 ,成功后可以看得到我們建的空數據庫里面已經有自動生成的表了,並且在輸出窗口中可以看到任務執行輸出的:

Hello world from Hangfire!

在啟動的頁面地址后面加上  /hangfire,就可以看到Dashboard頁面。

 

添加日志功能

在遇到任務失敗的時候日志能幫助我們更好的查找問題。

Hangfire支持一下日志框架(並且可以自動識別項目中的引用,為我們記錄日志):

  1. Serilog
  2. NLog
  3. Log4Net
  4. EntLib Logging
  5. Loupe
  6. Elmah

如果項目中引用了多個日志框架,日志可能會失敗,你可以使用下面的代碼來配置想使用的日志框架

GlobalConfiguration.Configuration
    .UseSerilogLogProvider()
    .UseNLogLogProvider()
    .UseLog4NetLogProvider()
    .UseEntLibLogProvider()
    .UseLoupeLogProvider()
    .UseElmahLogProvider();

我用了NLog,這里為了讓其他同事更清楚我配置了日志功能,所以盡快Hangfire可以自動識別,我還是把這一句加上了。

 

 然后我無情的發現日志沒有生成,忘記了NLog的配置文件,添加上就好了。

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      internalLogLevel="Debug" internalLogFile="c:\temp\nlog-internal.log">
  <!--throwExceptions 打印出Nlog的內部錯誤-->

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
    <!--文件-->
    <target name="fileLog" xsi:type="File"  fileName="${basedir}/logs/${date:format=yyyyMM}/${shortdate}.log" layout="${longdate} ${logger} ${level:uppercase=true} ${message}   ${exception:format=ToString}" />
  
  </targets>

  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
    <logger name="*" minlevel="Info" writeTo="fileLog" />
 
  </rules>
</nlog>

日志成功生成

 

 

 

 

 

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM