明净

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用Bootstrap来通过配置启动SuperSocket的时候总是显示Failed to initialize!  ,

官网配置中

<superSocket>
    <servers>
      <server name="TelnetServer"
          serverType="SuperSocket.QuickStart.TelnetServer_StartByConfig.TelnetServer, SuperSocket.QuickStart.TelnetServer_StartByConfig"
          ip="Any" port="2020">
      </server>
    </servers>
</superSocket>
serverType 要改成自己的命名空间和类名,改完还是一样的错误 

所以需要配置supersocket中带的log4net日志,查看错误信息。好多文档都看不懂,自己写一个希望对初用者有些帮助。。

 

第一步  NuGet 导入 log4net包

 

第二步 从配置文件中读取log4net的配置(配置见第三步)

a,载入app.config或 web.config配置       log4net.Config.XmlConfigurator.Configure();

b,载入单独的配置文件: log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("Config/log4net.config")); 

 

第三步 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
当前log4net配置, 是第三方的额外加的log4net节点,当前程序无法识别,所以在这儿要添加解析log4net的类名即:log4net.Config.Log4NetConfigurationSectionHandler <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <appSettings> </appSettings> <log4net> <root> level级别 (高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低)
设置为WARN 表示"OFF, FATAL, ERROR ,WARN 都写入日志,INFO DEBUG 不会写入日志
设置为ALL表示OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL都写入日志
<level value="ALL" /> <!--定义日志的输出媒介,如下定义四种方式 文件形式记录日志控制台显示日志Windows事件日志数据库日志--> <!--文件形式记录日志--> <appender-ref ref="RollingLogFileAppender" /> <!--<appender-ref ref="LogFileInfoAppender" />--> <!--<appender-ref ref="LogFileErrorAppender" />--> <!--控制台显示日志--> <!--<appender-ref ref="ConsoleAppender" />--> <!--Windows事件日志--> <!--<appender-ref ref="EventLogAppender" />--> <!--数据库日志--> <!--<appender-ref ref="AdoNetAppender_Oracle" />--> </root> <!--定义输出到文件中--> appender追加者追加器
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="test.txt"/> 写入文件 <appendToFile value="true"/> true追加 <maxSizeRollBackups value="10"/> 最多回滚10 即10个文件 <maximumfileSize value="1024KB"/> 每个文件最大1M <rollingStyle value="Size"/> 回滚样式根据文件大小Size回滚 每次都往test.txt中写,当文件大于1M 则把当前文件另存为test1.txt,重建test.txt继续写 最大10个文件,多于则10个循环覆盖
<staticLogFileName value="true"/> 静态文件名 当然还可以动态文件名,如日期 日志布局,,即日志格式,%date {2018-09-28 11:49:47,611} [%thread%]{[1]} %-5level{DEBUG或ERROR等。} %logger {日志标记,初始化的时候赋值} - %message{调用的时候赋的值} %newline {换行}
<layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread%] %-5level %logger - %message%newline"/> </layout> </appender> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>

 第四步,写日志

ILog logWriter=log4net.LogManager.GetLogger("DemoWriter");

logWriter.Debug("调试级别消息");

logWriter.Error("错误级别消息");

 

以上是学习过程=============================================

Config/log4net.config 内容

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>

  <log4net>
    
    <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
        <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="ERROR" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />
        <file value="Logs\err.log" />
        <encoding value="utf-8"/>
        <preserveLogFileNameExtension value="true" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
        </layout>
    </appender>
    <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
        <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="INFO" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />
        <file value="Logs\info.log" />
        <encoding value="utf-8"/>
        <preserveLogFileNameExtension value="true" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
        </layout>
    </appender>
    <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
        <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="DEBUG" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />
        <file value="Logs\debug.log" />
        <encoding value="utf-8"/>
        <preserveLogFileNameExtension value="true" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
        </layout>
    </appender>
    <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
        <filter type="log4net.Filter.LevelMatchFilter">
            <levelToMatch value="INFO" />
        </filter>
        <filter type="log4net.Filter.DenyAllFilter" />
        <file value="Logs\perf.log" />
        <encoding value="utf-8"/>
        <preserveLogFileNameExtension value="true" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <datePattern value="yyyyMMdd" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date %logger - %message%newline" />
        </layout>
    </appender>
    <root>
      <!-- 默认日志对象级别 -->
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="ALL" />
        <appender-ref ref="errorAppender" />
        <appender-ref ref="infoAppender" />
        <appender-ref ref="debugAppender" />
    </root>
    <logger name="Performance" additivity="false">
      <level value="ALL" />
      <appender-ref ref="perfAppender" />
    </logger>
</log4net>
</configuration>

 

using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
using System;using System.IO;namespace SuperSocketDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key to start the server!");
        载入log4net配置文件
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("Config/log4net.config"));

            var bootstrap = BootstrapFactory.CreateBootstrap();

            if (!bootstrap.Initialize())
            {
                Console.WriteLine("Failed to initialize!");
                Console.ReadKey();
                return;
            }

            var result = bootstrap.Start();

            Console.WriteLine("Start result: {0}!", result);

            if (result == StartResult.Failed)
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Press key 'q' to stop it!");

            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            }

            Console.WriteLine();

            //Stop the appServer
            bootstrap.Stop();

            Console.WriteLine("The server was stopped!");
            Console.ReadKey();



        }
    }
}

 

 

运行

查看   test.txt 文件内容

2018-09-28 11:49:47,611 [1] DEBUG DemoWriter - 调试级别消息
2018-09-28 11:49:47,651 [1] ERROR DemoWriter - 错误级别消息

 

 

通过以上学习,配置好supersocket的log4net发现日志

2018-09-28 11:54:54,262 [1] ERROR DefaultBootstrap - Failed to create server instance TelnetServer!
System.IO.FileNotFoundException: 未能加载文件或程序集“SuperSocketDemo”或它的某一个依赖项。系统找不到指定的文件。
文件名:“SuperSocketDemo”
   在 System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   在 System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   在 System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   在 System.Type.GetType(String typeName, Boolean throwOnError)
   在 SuperSocket.SocketEngine.DefaultBootstrap.CreateWorkItemInstance(String serviceTypeName, StatusInfoAttribute[] serverStatusMetadata)
   在 SuperSocket.SocketEngine.DefaultBootstrap.InitializeAndSetupWorkItem(WorkItemFactoryInfo factoryInfo)
。。。。。。。。。。。。。。。。

找不到程序集,原来是因为我改过项目名称,没改程序集名称。改了之后,,supersocket终于配置好了

 

posted on 2018-09-28 12:04  明净  阅读(996)  评论(0编辑  收藏  举报