/// <summary> /// Demonstrates how you can run a worker once, using a dead man's switch /// </summary> public static async Task Main() { // This example uses NLog, but it only requires a trivial amount of code to use any other logging library. var loggerFactory = new NLoggerFactory(); // You can also use Create() which disables logging var runner = DeadManSwitchRunner.Create(loggerFactory); var worker = new ExampleWorker(); using (var cancellationTokenSource = new CancellationTokenSource()) { var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(60) }; var run = runner.RunAsync(worker, options, cancellationTokenSource.Token); // if you want to cancel at some point: cancellationTokenSource.Cancel(); var result = await run.ConfigureAwait(false); Debug.Assert(result == Math.PI); } }
/// <summary> /// Demonstrates how to run (and stop) an infinite worker, using a dead man's switch /// </summary> public static async Task Main() { // This example uses NLog, but it only requires a trivial amount of code to use any other logging library var loggerFactory = new NLoggerFactory(); var infiniteRunner = InfiniteDeadManSwitchRunner.Create(loggerFactory); var worker = new ExampleInfiniteWorker(); using (var cancellationTokenSource = new CancellationTokenSource()) { var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(60) }; // do not await this, it will never complete until you cancel the token var run = infiniteRunner.RunAsync(worker, options, cancellationTokenSource.Token); // let it run for 10s. await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token).ConfigureAwait(false); // now stop the infinite worker cancellationTokenSource.Cancel(); // let it finish gracefully await run.ConfigureAwait(false); } }
public void GetLoggerTest() { var logger = new NLoggerFactory().GetLogger <NLoggerFactoryTests>(); Assert.IsNotNull(logger); Assert.IsInstanceOf <ILogger>(logger); }
private static void RegisterLogger(Container container) { var configPath = Path.Combine(Directory.GetCurrentDirectory(), NLogConfigName); var logFactory = new NLoggerFactory(configPath); container.RegisterInstance <ILoggerFactory>(logFactory); container.Register(() => logFactory.CreateLogger(LogNames.MainLog)); }
public static void Init(TestContext context) { //CleanUp нельзя делать, нужно самому смотреть, что все ок. CleanUp(); var factory = new NLoggerFactory("NLog.config"); _logger = factory.CreateLogger("main"); }
public void OperateLogToDbTest() { var testModel = new TestModel { Id = Guid.NewGuid(), TestTitle = "测试操作日志", TestNumber = 2, TestDateTime = DateTime.Now }; logFactory = new NLoggerFactory(); //创建日志对象 log = logFactory.CreateLogger("operateLog"); //日志数据赋值 var actionLog = new OperateLog() { Id = Guid.NewGuid(), Ip = "127.0.0.1", OperatorId = Guid.NewGuid().ToString(), OperAccount = "Account1", OperBranch = "运营1", OperType = OperatorType.Member, OperRemark = "操作备注", MemberId = Guid.NewGuid().ToString(), OriginalValue = JsonUtil.Serialize(new { Model = typeof(TestModel).Name, EntityId = testModel.Id, Title = testModel.TestTitle, 价格 = 99.0 }), ModifiedValue = JsonUtil.Serialize(new { Title = testModel.TestTitle + "修改后", 价格 = 59.0 }), ModelType = typeof(TestModel).FullName, Action = HandleActionType.Modify, SubAction = SubHandleActionType.None, Business = "订单模块", Remark = "这是备注说明", CreateTime = DateTime.Now }; //插入数据库 log.LogData(actionLog); Thread.Sleep(2000); }
/// <summary> /// Demonstrates how you can run a worker once, using a dead man's switch /// </summary> public static async Task Main() { var loggerFactory = new NLoggerFactory(); var runner = DeadManSwitchRunner.Create(loggerFactory); var worker = new ExampleWorker(); using (var cancellationTokenSource = new CancellationTokenSource()) { var options = new DeadManSwitchOptions { Timeout = TimeSpan.FromSeconds(60) }; var run = runner.RunAsync(worker, options, cancellationTokenSource.Token); // if you want to cancel at some point: cancellationTokenSource.Cancel(); var result = await run.ConfigureAwait(false); Debug.Assert(result == Math.PI); } }
public BasicTest() { logFactory = new NLoggerFactory(); log = logFactory.CreateLogger <BasicTest>(); }
static LogManager() { loggerFactory = new NLoggerFactory(); }