예제 #1
0
        public Task StartAsync()
        {
            _logger.Info($"start server: {_ip}:{_port}\r\n");
            if (_ip == "0.0.0.0")
            {
                _ip = JimuHelper.GetLocalIPAddress();
            }

            var builder = new WebHostBuilder()
                          .UseKestrel()
                          .UseIISIntegration()
                          .UseSetting("detailedErrors", "true")
                          .UseContentRoot(Directory.GetCurrentDirectory())
                          .UseUrls($"http://{_ip}:{_port}")
                          .ConfigureServices(services =>
            {
                services.AddSingleton <IStartup>(new Startup(new ConfigurationBuilder().Build(), _middlewares, _serviceEntryContainer, _logger));
            })
                          .UseSetting(WebHostDefaults.ApplicationKey, typeof(Startup).GetTypeInfo().Assembly.FullName)
                          //.UseStartup<Startup>()
            ;

            //.UseStartup<Startup>()
            //.Build();
            //_builderAction?.Invoke(builder);
            var host = builder.Build();

            host.Run();
            var endpoint = new IPEndPoint(IPAddress.Parse(_ip), _port);

            _logger.Info($"server start successfuly, address is: {endpoint}\r\n");
            return(Task.CompletedTask);
        }
예제 #2
0
 public IPEndPoint GetServerAddress()
 {
     if (_serverIp == "0.0.0.0")
     {
         return(new IPEndPoint(IPAddress.Parse(JimuHelper.GetLocalIPAddress()), _serverPort));
     }
     return(new IPEndPoint(IPAddress.Parse(_serverIp), _serverPort));
 }
예제 #3
0
        public async Task StartAsync()
        {
            _logger.Info($"start server: {_serverIp}:{_serverPort}");
            var bossGroup   = new MultithreadEventLoopGroup();
            var workerGroup = new MultithreadEventLoopGroup(4);
            var bootstrap   = new ServerBootstrap();

            bootstrap
            .Group(bossGroup, workerGroup)
            //.Group(bossGroup, workerGroup)
            .Channel <TcpServerSocketChannel>()
            .Option(ChannelOption.SoBacklog, 100)
            .Option(ChannelOption.Allocator, PooledByteBufferAllocator.Default)
            .ChildHandler(new ActionChannelInitializer <IChannel>(channel =>
            {
                var pipeline = channel.Pipeline;
                pipeline.AddLast(new LengthFieldPrepender(4));
                pipeline.AddLast(new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4));
                pipeline.AddLast(new ReadServerMessageChannelHandlerAdapter(_logger));
                pipeline.AddLast(new ServerHandlerChannelHandlerAdapter(async(context, message) =>
                {
                    await OnReceived(context, message);
                }, _logger));
            }));

            //var endpoint = new IPEndPoint(IPAddress.Parse(this.addre), this._port);
            //_channel = await bootstrap.BindAsync(_address.CreateEndPoint()); // bind with ip not support in docker, will not connected
            if (_serverIp != "0.0.0.0")
            {
                var endpoint = new IPEndPoint(IPAddress.Parse(this._serverIp), this._serverPort);
                _channel = await bootstrap.BindAsync(endpoint); // bind with ip not support in docker, will not connected

                _logger.Info($"server start successfuly, address is: {_serverIp}:{_serverPort}");
            }
            else
            {
                _channel = await bootstrap.BindAsync(_serverPort);

                _logger.Info($"server start successfuly, address is: {JimuHelper.GetLocalIPAddress()}:{_serverPort}");
            }
        }
예제 #4
0
        //void UseFileCofnig(ILoggerRepository rep)
        //{
        //    var type = MethodBase.GetCurrentMethod().DeclaringType;
        //    if (type != null)
        //    {
        //        var ns = type.Namespace;
        //        Assembly assembly = Assembly.GetExecutingAssembly();
        //        string resourceName = ns + ".log4net.config";
        //        Stream stream = assembly.GetManifestResourceStream(resourceName);
        //        XmlConfigurator.Configure(rep, stream);
        //    }
        //}

        //void UseCodeConfig(Hierarchy repository, string type)
        void UseCodeConfig(Hierarchy repository, LogLevel logLevel)
        {
            var ip = JimuHelper.GetLocalIPAddress();

            if (_options.EnableFileLog && (_options.FileLogLevel & logLevel) == logLevel)
            {
                PatternLayout layout = new PatternLayout
                {
                    ConversionPattern = "%date{yyyy-MM-dd HH:mm:ss.fff} %-5p [" + ip + "] %m%n"
                };
                layout.ActivateOptions();

                RollingFileAppender roller = new RollingFileAppender
                {
                    AppendToFile = false
                };
                var path = _options.EnableFileLog ? _options.FileLogPath : "log";
                roller.File = $@"{path}/{logLevel.ToString().ToLower()}/";
                roller.PreserveLogFileNameExtension = true;
                roller.StaticLogFileName            = false;
                roller.MaxSizeRollBackups           = 0;
                roller.DatePattern  = $@"yyyyMMdd"".log""";
                roller.RollingStyle = RollingFileAppender.RollingMode.Date;
                roller.Layout       = layout;
                roller.MaxFileSize  = 10000000;
                switch (logLevel)
                {
                case LogLevel.Debug:
                    roller.Threshold = Level.Debug;
                    break;

                case LogLevel.Info:
                    roller.Threshold = Level.Info;
                    break;

                case LogLevel.Warn:
                    roller.Threshold = Level.Warn;
                    break;

                case LogLevel.Error:
                    roller.Threshold = Level.Error;
                    break;
                }
                roller.ActivateOptions();
                repository.Root.AddAppender(roller);
            }

            if (_options.EnableConsoleLog && (_options.ConsoleLogLevel & logLevel) == logLevel)
            {
                //ManagedColoredConsoleAppender managedColoredConsoleAppender = new
                ManagedColoredConsoleAppender console = new ManagedColoredConsoleAppender();
                PatternLayout layoutConsole           = new PatternLayout
                {
                    ConversionPattern = "%n%date{yyyy-MM-dd HH:mm:ss.fff} %-5level [" + ip + "] %m",
                };
                switch (logLevel)
                {
                case LogLevel.Debug:
                    //console.Threshold = Level.Debug;
                    console.AddFilter(new LevelRangeFilter()
                    {
                        LevelMax = Level.Debug, LevelMin = Level.Debug
                    });
                    //console.AddFilter(new LevelMatchFilter() { LevelToMatch = Level.Debug, AcceptOnMatch = true });
                    break;

                case LogLevel.Info:
                    //console.Threshold = Level.Info;
                    console.AddFilter(new LevelRangeFilter()
                    {
                        LevelMax = Level.Info, LevelMin = Level.Info
                    });
                    //console.AddFilter(new LevelMatchFilter() { LevelToMatch = Level.Info, AcceptOnMatch = true });
                    break;

                case LogLevel.Warn:
                    //console.Threshold = Level.Warn;
                    console.AddFilter(new LevelRangeFilter()
                    {
                        LevelMax = Level.Warn, LevelMin = Level.Warn
                    });
                    //console.AddFilter(new LevelMatchFilter() { LevelToMatch = Level.Warn, AcceptOnMatch = true });
                    break;

                case LogLevel.Error:
                    //console.Threshold = Level.Error;
                    console.AddFilter(new LevelRangeFilter()
                    {
                        LevelMax = Level.Error, LevelMin = Level.Error
                    });
                    //console.AddFilter(new LevelMatchFilter() { LevelToMatch = Level.Error });
                    break;
                }
                console.AddMapping(
                    new ManagedColoredConsoleAppender.LevelColors {
                    Level = Level.Error, ForeColor = ConsoleColor.DarkRed
                });
                console.AddMapping(
                    new ManagedColoredConsoleAppender.LevelColors {
                    Level = Level.Warn, ForeColor = ConsoleColor.DarkYellow
                });

                layoutConsole.ActivateOptions();
                console.Layout = layoutConsole;
                console.ActivateOptions();
                repository.Root.AddAppender(console);
            }

            //MemoryAppender memory = new MemoryAppender();
            //memory.ActivateOptions();
            //repository.Root.AddAppender(memory);

            //repository.Root.Level = Level.Debug;
            repository.Configured = true;
        }
예제 #5
0
 public NLogger(JimuNLogOptions options = null)
 {
     options = options ?? new JimuNLogOptions {
         EnableConsoleLog = true
     };
     if (!string.IsNullOrEmpty(options.Configuration))
     {
         N.LogManager.Configuration = new N.Config.XmlLoggingConfiguration("nlog.config");
     }
     else
     {
         var config = new N.Config.LoggingConfiguration();
         var ip     = JimuHelper.GetLocalIPAddress();
         if (options.EnableFileLog)
         {
             var fileConf = new N.Targets.FileTarget("jimuLogFile")
             {
                 FileName         = "./log/${level:lowercase=true}/${shortdate}.log",
                 ArchiveAboveSize = 10000000,
                 Layout           = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} [" + ip + "] ${message}"
             };
             if (options.FileLogPath != null)
             {
                 fileConf.FileName = options.FileLogPath + "/${level:lowercase=true}/${shortdate}.log";
             }
             if ((options.FileLogLevel & LogLevel.Error) == LogLevel.Error)
             {
                 config.AddRuleForOneLevel(N.LogLevel.Error, fileConf);
             }
             if ((options.FileLogLevel & LogLevel.Warn) == LogLevel.Warn)
             {
                 config.AddRule(N.LogLevel.Warn, N.LogLevel.Error, fileConf);
             }
             if ((options.FileLogLevel & LogLevel.Info) == LogLevel.Info)
             {
                 config.AddRule(N.LogLevel.Info, N.LogLevel.Error, fileConf);
             }
             if ((options.FileLogLevel & LogLevel.Debug) == LogLevel.Debug)
             {
                 config.AddRule(N.LogLevel.Debug, N.LogLevel.Error, fileConf);
             }
         }
         if (options.EnableConsoleLog)
         {
             var consoleLog = new N.Targets.ConsoleTarget("jimuLogconsole")
             {
                 Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} [" + ip + "] ${message}"
             };
             if ((options.ConsoleLogLevel & LogLevel.Error) == LogLevel.Error)
             {
                 config.AddRuleForOneLevel(N.LogLevel.Error, consoleLog);
             }
             if ((options.ConsoleLogLevel & LogLevel.Warn) == LogLevel.Warn)
             {
                 config.AddRule(N.LogLevel.Warn, N.LogLevel.Error, consoleLog);
             }
             if ((options.ConsoleLogLevel & LogLevel.Info) == LogLevel.Info)
             {
                 config.AddRule(N.LogLevel.Info, N.LogLevel.Error, consoleLog);
             }
             if ((options.ConsoleLogLevel & LogLevel.Debug) == LogLevel.Debug)
             {
                 config.AddRule(N.LogLevel.Debug, N.LogLevel.Error, consoleLog);
             }
         }
         N.LogManager.Configuration = config;
     }
     _logger = N.LogManager.GetLogger("*", typeof(N.LogManager));
 }
예제 #6
0
파일: NLogger.cs 프로젝트: littlewrong/jimu
        public NLogger(JimuLog4netOptions options = null)
        {
            options = options ?? new JimuLog4netOptions {
                EnableConsoleLog = true
            };
            var config = new NLog.Config.LoggingConfiguration();
            var ip     = JimuHelper.GetLocalIPAddress();

            if (options.EnableFileLog)
            {
                var fileConf = new NLog.Targets.FileTarget("jimuLogFile")
                {
                    FileName         = ".\\log\\${level:lowercase=true}\\${shortdate}.log",
                    ArchiveAboveSize = 10000000,
                    Layout           = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} [" + ip + "] ${message}"
                };
                if (options.FileLogPath != null)
                {
                    fileConf.FileName = options.FileLogPath + "\\${level:lowercase=true}\\${shortdate}.log";
                }
                if ((options.FileLogLevel & LogLevel.Error) == LogLevel.Error)
                {
                    config.AddRuleForOneLevel(NLog.LogLevel.Error, fileConf);
                }
                if ((options.FileLogLevel & LogLevel.Warn) == LogLevel.Warn)
                {
                    //config.AddRuleForOneLevel(NLog.LogLevel.Warn, fileConf);
                    config.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Error, fileConf);
                }
                if ((options.FileLogLevel & LogLevel.Info) == LogLevel.Info)
                {
                    config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Error, fileConf);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Info, fileConf);
                }
                if ((options.FileLogLevel & LogLevel.Debug) == LogLevel.Debug)
                {
                    config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Error, fileConf);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Debug, fileConf);
                }
            }

            if (options.EnableConsoleLog)
            {
                var consoleLog = new NLog.Targets.ConsoleTarget("jimuLogconsole")
                {
                    Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} [" + ip + "] ${message}"
                };
                if ((options.ConsoleLogLevel & LogLevel.Error) == LogLevel.Error)
                {
                    config.AddRuleForOneLevel(NLog.LogLevel.Error, consoleLog);
                }
                if ((options.ConsoleLogLevel & LogLevel.Warn) == LogLevel.Warn)
                {
                    config.AddRule(NLog.LogLevel.Warn, NLog.LogLevel.Error, consoleLog);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Warn, consoleLog);
                }
                if ((options.ConsoleLogLevel & LogLevel.Info) == LogLevel.Info)
                {
                    config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Error, consoleLog);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Info, consoleLog);
                }
                if ((options.ConsoleLogLevel & LogLevel.Debug) == LogLevel.Debug)
                {
                    config.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Error, consoleLog);
                    //config.AddRuleForOneLevel(NLog.LogLevel.Debug, consoleLog);
                }
            }
            NLog.LogManager.Configuration = config;
            _logger = NLog.LogManager.GetLogger("jimuLogger");
        }
예제 #7
0
 public ConsoleLogger()
 {
     _ip = JimuHelper.GetLocalIPAddress();
 }