heshaofeng
2025-12-29 266e4bf654c55ce2f7e9271048e4625f1b8b49f6
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_WMSServer/Program.cs
@@ -1,4 +1,6 @@
using System.Net.Security;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using Autofac;
using Autofac.Core;
@@ -11,7 +13,11 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Quartz;
using Quartz.Impl;
using Quartz.Spi;
using Serilog;
using WIDESEA.QuartzJob;
using WIDESEA_Core;
using WIDESEA_Core.Authorization;
using WIDESEA_Core.BaseServices;
@@ -21,9 +27,11 @@
using WIDESEA_Core.Helper;
//using WIDESEA_Core.HostedService;
using WIDESEA_Core.Middlewares;
using WIDESEA_DTO.Quartz;
using WIDESEA_WMSServer;
using WIDESEA_WMSServer.Filter;
using Quartz;
using WIDESEA_WMSServer.Jobs;
using WIDESEAWCS_Core.LogHelper;
var builder = WebApplication.CreateBuilder(args);
@@ -33,6 +41,36 @@
//  .WriteTo.Console()
//  .WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
//  .CreateLogger();
AppDomain.CurrentDomain.FirstChanceException += (sender, e) =>
{
    bool flag = e.Exception.TargetSite?.DeclaringType?.Namespace?.StartsWith("WIDESEA") == true;
    if (e.Exception is not OperationCanceledException && flag)
    {
        string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Log\\Exception");
        Exception ex = e.Exception;
        StringBuilder builder = new StringBuilder();
        builder.Append("错误信息:");
        builder.Append(ex.Message);
        builder.Append(Environment.NewLine);
        builder.Append("错误源:");
        builder.Append(ex.Source);
        builder.Append(Environment.NewLine);
        builder.Append("错误堆栈:");
        builder.Append(ex.StackTrace);
        builder.Append(Environment.NewLine);
        builder.Append("错误类型:");
        builder.Append(ex.GetType().ToString());
        builder.Append(Environment.NewLine);
        builder.Append("错误方法:");
        builder.Append(ex.TargetSite?.ToString());
        builder.Append(Environment.NewLine);
        builder.Append(Environment.NewLine);
        LoggerHelper.WriteError(e.Exception, folderPath);
    }
};
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Information() // è®¾ç½®å…¨å±€æœ€ä½Žæ—¥å¿—级别为 Information
@@ -47,7 +85,7 @@
    .WriteTo.File( // é…ç½®è¾“出到文件
        path: "logs/log-.txt", // æ—¥å¿—文件路径,会自动创建 logs æ–‡ä»¶å¤¹
        rollingInterval: RollingInterval.Day, // æŒ‰å¤©æ»šåŠ¨æ—¥å¿—æ–‡ä»¶
        retainedFileCountLimit: 7, // æœ€å¤šä¿ç•™7天的日志
        retainedFileCountLimit: 365, // æœ€å¤šä¿ç•™7天的日志
        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
    )
    .CreateLogger();
@@ -148,13 +186,16 @@
    client.Timeout = TimeSpan.FromSeconds(30);
});
builder.Services.AddSingleton<IJobFactory, JobFactory>();
builder.Services.AddTransient<AgvTaskJob>();//Job使用瞬时依赖注入
builder.Services.AddTransient<InventoryLockJob>();//Job使用瞬时依赖注入
builder.Services.AddSingleton<ISchedulerCenter, SchedulerCenterServer>();
builder.Services.AddQuartz(q =>
{
    
    var jobKey = new JobKey("ErpJob");
    q.AddJob<ErpJob>(opts => opts.WithIdentity(jobKey));
    // æ·»åŠ è§¦å‘å™¨ - æ¯å¤©10:00, 14:00, 20:00执行
    q.AddTrigger(opts => opts
@@ -162,21 +203,20 @@
        .WithIdentity("ErpJob-trigger")
        .WithCronSchedule("0 0 10,14,20 * * ?"));
    var inventoryLockJobKey = new JobKey("InventoryLockJob");
    q.AddJob<InventoryLockJob>(opts => opts.WithIdentity(inventoryLockJobKey));
    //var inventoryLockJobKey = new JobKey("InventoryLockJob");
    //q.AddJob<InventoryLockJob>(opts => opts.WithIdentity(inventoryLockJobKey));
    q.AddTrigger(opts => opts
        .ForJob(inventoryLockJobKey)
        .WithIdentity("InventoryLockJob-trigger")
        .WithCronSchedule("0 0/10 * * * ?")); // æ¯10分钟执行一次
    //q.AddTrigger(opts => opts
    //    .ForJob(inventoryLockJobKey)
    //    .WithIdentity("InventoryLockJob-trigger")
    //    .WithCronSchedule("0 0/10 * * * ?")); // æ¯10分钟执行一次
});
builder.Services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);
var app = builder.Build();
app.UseQuartzJobMildd();
app.UseMiniProfiler();//性能分析器
app.ConfigureApplication();//配置文件
app.UseApplicationSetup();//启动配置