簡體   English   中英

俄語字符串顯示在 log.txt 中 - 這個:???? C# 使用 log.net 時

[英]A Russian string is displayed in the log.txt - this: ???? C# when used log4net

我嘗試打印俄語字符串進行記錄。 使用 log.net。 這是我的代碼:

  1. 在 Web.config 中寫入消息的文件

    <configuration> <appSettings> <add key="logFilePath" value="~/logTest.log" /> </appSettings> </configSections> <log.net> <appender name="RollingFileAppender" type="log.net.Appender.RollingFileAppender"> <file value="C:\log.txt"/> <encoding value="utf-8" /> <appendToFile value="true"/> <rollingStyle value="Size"/> <maxSizeRollBackups value="5"/> <maximumFileSize value="2MB"/> <staticLogFileName value="true"/> <layout type="log.net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %level %logger - %message%newline%exception%newline------------------------- ---------------------------------------"/> </layout> </appender> <root> <level value="WARN"/> <appender-ref ref="RollingFileAppender"/> </root> </log.net>
  2. class 處理寫入日志和配置:

     namespace Utils { public class ClsLog { private static readonly log.net.ILog log = log.net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static bool isInitialized = false; public static void WriteLog(enmLogType logType, string message) { Initialize(); log.Debug(message); } private static void Initialize() { if (isInitialized) return; string path = ConfigurationManager.AppSettings["logFilePath"]; string currentDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); currentDir = currentDir.Substring(6); currentDir += "\\.."; FileInfo finfo = new FileInfo(currentDir + "\\log.net.config"); log.net.Config.XmlConfigurator.ConfigureAndWatch(finfo); log.net.Repository.Hierarchy.Hierarchy h = (log.net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository(); foreach (IAppender a in h.Root.Appenders) { if (a is FileAppender) { FileAppender fa = (FileAppender)a; // Programmatically set this to the desired location here if (path;= null) { string logFileLocation = path. fa;File = logFileLocation. fa;ActivateOptions(); break; } } } isInitialized = true; } } }
  3. 用俄語打印字符串:

     public void print() { Utils.ClsLog.WriteLog(enmLogType.Debug, "Русский"); }

但是文件日志中的 output 是:

2023-01-16 12:52:17,282  DEBUG ???????  

我將如何得到這個結果?:

2023-01-16 12:52:17,282  DEBUG Русский

以及我如何知道創建文件時使用的是什么編碼?

以下行:

 <encoding value="utf-8" />

隱藏在項目中。 它應該被添加到log.net.Config中,而不是在Web.Config中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM