电子围栏推送服务
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

103 lines
3.9KB

  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Serialization;
  3. using Serilog;
  4. using TelpoDataService.Util.Clients;
  5. using TelpoPush.Fence.Worker;
  6. using TelpoPush.Fence.Worker.Common;
  7. using TelpoPush.Fence.Worker.Handlers;
  8. using TelpoPush.Fence.Worker.Models.Config;
  9. using TelpoPush.Fence.Worker.Service.Cache;
  10. using TelpoPush.Fence.Worker.Service.Mq;
  11. try
  12. {
  13. #region 日志
  14. //using Serilog.Events;
  15. //using Serilog;
  16. //using Microsoft.AspNetCore.Builder;
  17. //Log.Logger = new LoggerConfiguration()
  18. //#if DEBUG
  19. // .MinimumLevel.Debug()
  20. //#else
  21. // .MinimumLevel.Information()
  22. //#endif
  23. // .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
  24. // .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
  25. // .Enrich.FromLogContext()
  26. // //.Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("Microsoft")))//过滤
  27. // .WriteTo.Async(c => c.File("/var/telpo_pushthird_ssl2/logs/infos/info.log",
  28. // restrictedToMinimumLevel: LogEventLevel.Information,
  29. // rollingInterval: RollingInterval.Day,//滚动策略(天)
  30. // //fileSizeLimitBytes: 20971520, //设置单个文件大小为3M 默认1G
  31. // rollOnFileSizeLimit: true, //超过文件大小后创建新的
  32. // retainedFileCountLimit: 7,//默认31,意思就是只保留最近的31个日志文件"
  33. // outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}[{Level:u3}] [Thread-{ThreadId}] [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"
  34. // )
  35. // )
  36. // .WriteTo.Async(c => c.File("/var/telpo_pushthird_ssl2/logs/errors/errors.log",
  37. // restrictedToMinimumLevel: LogEventLevel.Error,
  38. // rollingInterval: RollingInterval.Day,
  39. // rollOnFileSizeLimit: true,
  40. // retainedFileCountLimit: 7,
  41. // outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff }[{Level:u3}] [Thread-{ThreadId}] [{SourceContext:l}] {Message:lj}{NewLine}{Exception}"
  42. // )
  43. // )
  44. // .WriteTo.Async(c => c.Console())
  45. // .CreateLogger();
  46. //选择配置文件appsetting.json
  47. var configuration = new ConfigurationBuilder()
  48. .SetBasePath(Directory.GetCurrentDirectory())
  49. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  50. .Build();
  51. Log.Logger = new LoggerConfiguration()
  52. .ReadFrom.Configuration(configuration)
  53. .Enrich.WithThreadId()
  54. .CreateLogger();
  55. #endregion
  56. Log.Information("Starting up");
  57. var builder = Host.CreateApplicationBuilder(args);
  58. var config = builder.Configuration;
  59. builder.Services.Configure<FenceConfig>(config.GetSection("FenceConfig"));
  60. builder.Services.Configure<ServiceConfig>(config.GetSection("ServiceConfig"));
  61. builder.Services.Configure<RedisConfig>(config.GetSection("Redis"));
  62. JsonSerializerSettings setting = new JsonSerializerSettings();
  63. JsonConvert.DefaultSettings = () =>
  64. {
  65. setting.DateFormatString = "yyyy-MM-dd HH:mm:ss";
  66. setting.ContractResolver = new CamelCasePropertyNamesContractResolver();
  67. return setting;
  68. };
  69. builder.Services.AddTelpoDataServices(opt =>
  70. {
  71. opt.TelpoDataUrl = config["ServiceConfig:TelpoDataUrl"];
  72. });
  73. builder.Services.AddSerilog();
  74. builder.Services.AddHttpClient();
  75. builder.Services.AddTransient<HttpHelperAsync>();
  76. builder.Services.AddSingleton<SqlMapper>();
  77. builder.Services.AddSingleton<RedisUtil>();
  78. builder.Services.AddSingleton<IKafkaService, KafkaService>();
  79. builder.Services.AddSingleton<KafkaSubscribe>();
  80. builder.Services.AddSingleton<MessageProducer>();
  81. builder.Services.AddSingleton<MqProcessMessage>();
  82. builder.Services.AddSingleton<FenceProcess>();
  83. builder.Services.AddHostedService<Worker>();
  84. var host = builder.Build();
  85. host.Run();
  86. }
  87. catch (Exception ex)
  88. {
  89. Log.Fatal(ex, "Application start-up failed");
  90. }
  91. finally
  92. {
  93. Log.CloseAndFlush();
  94. }