Exemple #1
0
 /// <summary> 默认构造函数 </summary>
 protected DTest()
 {
     Bootstrap = DBootstrap.Instance;
     Bootstrap.Initialize();
     //LogManager.ClearAdapter();
     LogManager.AddAdapter(new ConsoleAdapter());
 }
Exemple #2
0
        private static void Main(string[] args)
        {
            LogManager.AddAdapter(new ConsoleAdapter());
            LogManager.LogLevel(LogLevel.Debug);
            var logger  = LogManager.Logger <Program>();
            var servers = new ServiceCollection()
                          .AddJsonCoder()
                          .AddDotNetty()
                          .AddConsul("http://192.168.0.252:8500")
                          .AddClient();
            var provider = servers.BuildServiceProvider();

            provider.UseClient();
            Console.WriteLine("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                Task.Run(async() =>
                {
                    var proxy   = provider.GetService <IClientProxy>();
                    var service = proxy.Create <ITestContract>();
                    var t       = await service.Get(message);
                    logger.Info(t);
                });
                //var result = CodeTimer.Time("dotnetty test", 100, async () =>
                //{
                //    var proxy = MicroServices.GetService<IClientProxy>();
                //    var service = proxy.Create<ITestContract>();
                //    await service.Get(message);
                //}, 20);
                //logger.Info(result.ToString());
            }
        }
Exemple #3
0
 public IServiceProvider ConfigureServices(IServiceCollection services)
 {
     services.AddMvc();
     services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
     _bootstrap.BuilderHandler += builder => { builder.Populate(services); };
     _bootstrap.Initialize();
     MicroServiceRegister.Regist("localhost", 50145);
     LogManager.AddAdapter(new ConsoleAdapter());
     return(new AutofacServiceProvider(_bootstrap.Container));
 }
        private static void SetTcpAddpend()
        {
            var tcpAppender = Log4NetDefaultConfig.TcpAppender();

            if (tcpAppender == null)
            {
                return;
            }
            LogManager.RemoveAdapter(typeof(TcpAppender));
            LogManager.AddAdapter(new TcpLogAdapter(tcpAppender));
        }
Exemple #5
0
        public static void Start(params string[] args)
        {
            var services = new MicroBuilder()
                           .AddMicroClient(builder =>
            {
                builder
                .AddJsonCodec()
                .AddMessagePackCodec()
                //.AddProtoBufCodec()
                .AddSession()
                .AddHttpProtocol()
                .AddTcpProtocol()
                .AddWebSocketProtocol()
                .AddGrpcProtocol()
                //.AddNacos(opt =>
                //{
                //    opt.Host = "http://192.168.0.231:8848/";
                //    opt.Tenant = "ef950bae-865b-409b-9c3b-bc113cf7bf37";
                //})
                .AddConsul("http://192.168.0.231:8500")
                ;
            });

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Information);
                builder.AddConsole();
            });
            services.AddSingleton <DefaultAdapter>();
            services.AddSingleton <IService, ServieA>();
            services.AddSingleton <IService, ServieB>();
            var provider = services.BuildServiceProvider();

            LogManager.AddAdapter(provider.GetService <DefaultAdapter>());
            LogManager.Logger <Client>().Info("test");

            var logger = provider.GetService <ILogger <Client> >();

            logger.LogInformation("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }
                //SingleTest(provider, message, logger);
                CodeTimerTest(provider, message, logger);
            }
        }
Exemple #6
0
        public virtual IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services
            .AddMvc(options =>
            {
                //自定义异常捕获
                options.Filters.Add <DExceptionFilter>();
            })
            .AddJsonOptions(opts =>
            {
                //json序列化处理
                opts.SerializerSettings.Converters.Add(new DateTimeConverter());
            });

            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            _bootstrap.BuilderHandler += builder => { builder.Populate(services); };
            _bootstrap.Initialize();
            LogManager.AddAdapter(new ConsoleAdapter());
            return(new AutofacServiceProvider(_bootstrap.Container));
        }
Exemple #7
0
        public static void Start(params string[] args)
        {
            var services = new ServiceCollection()
                           .AddMicroClient(opt =>
            {
                opt.AddJsonCoder()
                .AddHttpProtocol()
                .AddTcpProtocol()
                .AddConsul("http://192.168.0.231:8500");
            });

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Debug);
                builder.AddConsole();
            });
            services.AddSingleton <DefaultAdapter>();
            services.AddSingleton <IService, ServieA>();
            services.AddSingleton <IService, ServieB>();
            var provider = services.BuildServiceProvider();

            LogManager.AddAdapter(provider.GetService <DefaultAdapter>());
            LogManager.Logger <Client>().Info("test");

            var logger = provider.GetService <ILogger <Client> >();

            logger.LogInformation("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }
                var msgArgs = message.Split(new[] { ",", ";", " " }, StringSplitOptions.RemoveEmptyEntries);
                int repeat = 1, thread = 1;
                var isNotice = true;
                if (msgArgs.Length > 1)
                {
                    repeat = msgArgs[1].CastTo(1);
                }
                if (msgArgs.Length > 2)
                {
                    thread = msgArgs[2].CastTo(1);
                }
                if (msgArgs.Length > 3)
                {
                    isNotice = msgArgs[3].CastTo(true);
                }
                message = msgArgs[0];
                //var watch = Stopwatch.StartNew();
                //var proxy = provider.GetService<IProxyFactory>();
                //var service = proxy.Create<ITestContract>();
                //if (isNotice)
                //{
                //    service.Notice(message).GetAwaiter().GetResult();
                //}
                //else
                //{
                //    var msg = service.Get(message).Result;
                //    logger.Debug(msg);
                //}
                //watch.Stop();
                //logger.Info(watch.ElapsedMilliseconds);
                Task.Run(async() =>
                {
                    var proxy   = provider.GetService <IProxyFactory>();
                    var service = proxy.Create <ITestContract>();

                    var result = await CodeTimer.Time("micro test", repeat, async() =>
                    {
                        try
                        {
                            if (isNotice)
                            {
                                await service.Notice(message);
                            }
                            else
                            {
                                var msg = await service.Get(message);
                                logger.LogInformation(msg);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, ex.Message);
                            throw;
                        }
                    }, thread);
                    logger.LogInformation(result.ToString());
                });
            }
        }
 /// <summary> 初始化日志模块 </summary>
 public override void LoggerInit()
 {
     LogManager.AddAdapter(new Log4NetAdapter());
 }
Exemple #9
0
 public TestBase()
 {
     LogManager.AddAdapter(new Log4NetAdapter());
 }
Exemple #10
0
        public static void Start(params string[] args)
        {
            var services = new MicroBuilder()
                           .AddMicroClient(builder =>
            {
                builder.AddJsonCoder()
                .AddSession()
                .AddHttpProtocol()
                .AddTcpProtocol()
                .AddNacos(opt =>
                {
                    opt.Host   = "http://192.168.0.231:8848/";
                    opt.Tenant = "ef950bae-865b-409b-9c3b-bc113cf7bf37";
                })
                //.AddConsul("http://192.168.0.231:8500")
                ;
            });

            services.AddLogging(builder =>
            {
                builder.SetMinimumLevel(LogLevel.Warning);
                builder.AddConsole();
            });
            services.AddSingleton <DefaultAdapter>();
            services.AddSingleton <IService, ServieA>();
            services.AddSingleton <IService, ServieB>();
            var provider = services.BuildServiceProvider();

            LogManager.AddAdapter(provider.GetService <DefaultAdapter>());
            LogManager.Logger <Client>().Info("test");

            var logger = provider.GetService <ILogger <Client> >();

            logger.LogInformation("请输入消息");
            while (true)
            {
                var message = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(message))
                {
                    continue;
                }
                var msgArgs = message.Split(new[] { ",", ";", " " }, StringSplitOptions.RemoveEmptyEntries);
                int repeat = 1, thread = 1;
                var isNotice = true;
                if (msgArgs.Length > 1)
                {
                    repeat = msgArgs[1].CastTo(1);
                }
                if (msgArgs.Length > 2)
                {
                    thread = msgArgs[2].CastTo(1);
                }
                if (msgArgs.Length > 3)
                {
                    isNotice = msgArgs[3].CastTo(true);
                }
                message = msgArgs[0];
                //var watch = Stopwatch.StartNew();
                //var proxy = provider.GetService<IProxyFactory>();
                //var service = proxy.Create<ITestContract>();
                //if (isNotice)
                //{
                //    service.Notice(message).GetAwaiter().GetResult();
                //}
                //else
                //{
                //    var msg = service.Get(message).Result;
                //    logger.Debug(msg);
                //}
                //watch.Stop();
                //logger.Info(watch.ElapsedMilliseconds);
                Task.Run(async() =>
                {
                    var proxy   = provider.GetService <IProxyFactory>();
                    var service = proxy.Create <ITestContract>();
                    //if (message.StartsWith("s"))
                    //{
                    //    var accessor = provider.GetService<IPrincipalAccessor>();
                    //    accessor.SetSession(new MicroSessionDto
                    //    {
                    //        UserId = RandomHelper.Random().Next(10000, 99999),
                    //        UserName = RandomHelper.RandomLetters(5),
                    //        Role = "admin"
                    //    });
                    //}

                    var result = await CodeTimer.Time("micro test", repeat, async() =>
                    {
                        try
                        {
                            var name = RandomHelper.RandomLetters(5);
                            var m    = $"hello {name}";
                            if (RandomHelper.Random().Next(10) > 5)
                            {
                                m           += " loged";
                                var accessor = provider.GetService <IPrincipalAccessor>();
                                accessor.SetSession(new MicroSessionDto
                                {
                                    UserId   = name,
                                    UserName = name,
                                    Role     = name
                                });
                            }
                            if (isNotice)
                            {
                                await service.Notice(m);
                            }
                            else
                            {
                                var msg = await service.Get(m);
                                logger.LogInformation(msg);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, ex.Message);
                            throw;
                        }
                    }, thread);
                    Console.WriteLine(result.ToString());
                });
            }
        }