static void InitLogHelper() { try { LogParameter logparameter = new LogParameter(); if (ConfigurationManager.AppSettings.AllKeys.Contains(LogHelper.LOGDESTINATIONKEY)) { logparameter.LogDestinationKey = ConfigurationManager.AppSettings[LogHelper.LOGDESTINATIONKEY].Trim(); } if (ConfigurationManager.AppSettings.AllKeys.Contains(LogHelper.AUTOFLUSHKEY)) { logparameter.AutoFlushKey = ConfigurationManager.AppSettings[LogHelper.AUTOFLUSHKEY].Trim(); } if (ConfigurationManager.AppSettings.AllKeys.Contains(LogHelper.LOGPATHKEY)) { logparameter.LogPathKey = ConfigurationManager.AppSettings[LogHelper.LOGPATHKEY].Trim(); } LogHelper.Init(logparameter); } catch (Exception e) { Trace.WriteLine(e.Message); } }
/// <summary> /// Init by configuration /// </summary> public static void Init(LogParameter logParameter) { if (isInitialized) { return; } string logPath = string.Empty; string logDestination = string.Empty; FileStream stream; FileMode fileMode; #if Debug LogDest logDest = LogDest.Debugger; #else LogDest logDest = LogDest.None; #endif // Get log destination try { logDestination = logParameter.LogDestinationKey.Trim(); int logDestSetting = 0; if (int.TryParse(logDestination, out logDestSetting)) { logDest = (LogDest)logDestSetting; } // auto flush setting Trace.AutoFlush = logParameter.AutoFlushKey.Trim() == "1"; } catch (Exception e) { Trace.WriteLine(e.Message); } switch (logDest) { case LogDest.None: { Trace.Listeners.Clear(); isInitialized = true; return; } case LogDest.Debugger: { //use default listener isInitialized = true; needOutputLog = true; return; } case LogDest.File: { //2 - File (Overwrite the old file and doesn't close it until application exits) Trace.Listeners.Clear(); fileMode = FileMode.Create; break; } case LogDest.FileAppend: { //3 - File (Append the log at the end of file and close it after each log output) Trace.Listeners.Clear(); fileMode = FileMode.Append; break; } default: Trace.WriteLine("### Wrong log destination! ###"); return; } // check log file path //path is null logPath = logParameter.LogPathKey.Trim(); if (string.IsNullOrEmpty(logPath)) { return; } //path has invalid char var pathCharArray = logPath.ToCharArray(); if (pathCharArray.Any(o => Path.GetInvalidPathChars().Contains(o))) { return; } //FileName has invalid char //note : invalid file name chars count is 41, //invalid path chars count is 36 //and , the top 36 of invalid file name chars are same as invalid path chars //so, first check path invalid chars, second check filename, only filename var filenameCharArray = Path.GetFileName(logPath).ToCharArray(); if (filenameCharArray.Any(o => Path.GetInvalidFileNameChars().Contains(o))) { return; } //EnvironmentVariables Path if (logPath.Contains('%')) { logPath = Environment.ExpandEnvironmentVariables(logPath); } //change relative path to absolute path. if (string.IsNullOrEmpty(Path.GetPathRoot(logPath))) { logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, logPath); } // log by file //risk:directory readonly;need administrator right to createfile;and so on //use try-catch try { if (!Directory.Exists(Path.GetDirectoryName(logPath))) { Directory.CreateDirectory(Path.GetDirectoryName(logPath)); } stream = File.Open(logPath, fileMode, FileAccess.Write, FileShare.ReadWrite); TextWriterTraceListener text = new TextWriterTraceListener(stream); //text.TraceOutputOptions = TraceOptions.DateTime; Trace.Listeners.Add(text); } catch (FileNotFoundException ex) { Trace.Write(ex); } needOutputLog = true; isInitialized = true; }