示例#1
0
        public static GrandfatherClockOptions ReadConfiguration()
        {
            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddJsonFile("appsettings.json");
            IConfiguration configuration = configurationBuilder.Build();
            var            options       = new GrandfatherClockOptions();

            configuration.Bind(options);
            return(options);
        }
 public ClockService(GrandfatherClockOptions options)
 {
     _millisecondsFactor = options.ChimeInterval;
     _options            = options;
     _timer = new Timer(GetMillisecondsToNextChime(_millisecondsFactor))
     {
         AutoReset = true
     };
     Log.Information($"Next chime will be at {DateTime.Now.AddMilliseconds(_timer.Interval)}");
     _timer.Elapsed += Chime;
 }
示例#3
0
        public static void Main(string[] args)
        {
            GrandfatherClockOptions options = ReadConfiguration();

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.File(options.LogDirectoryPath, rollingInterval: RollingInterval.Day)
                         .WriteTo.Console()
                         .CreateLogger();

            var builder = CreateHostBuilder(args);

            builder.RunAsTopshelfService(hc =>
            {
                hc.SetServiceName("GrandfatherClock");
                hc.SetDisplayName("Grandfather Clock");
                hc.SetDescription("Chimes every hour.");
            });
        }