使用Bootstrap來經過配置啓動SuperSocket的時候老是顯示Failed to initialize! ,web
官網配置中數據庫
<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包bootstrap
第二步 從配置文件中讀取log4net的配置(配置見第三步)app
a,載入app.config或 web.config配置 log4net.Config.XmlConfigurator.Configure();socket
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>
第四步,寫日誌ui
ILog logWriter=log4net.LogManager.GetLogger("DemoWriter");spa
logWriter.Debug("調試級別消息");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終於配置好了