private bool UpdateLogConfigFile() { var updated = false; //check for log config and create if not existing try { var requiredLogs = new[] {"Zone", "Bob", "Power", "Asset", "Rachelle", "Arena"}; LogConfig logConfig = new LogConfig(); if(File.Exists(_logConfigPath)) { using(var sr = new StreamReader(_logConfigPath)) { LogConfig.ConfigItem current = null; string line; while(!sr.EndOfStream && (line = sr.ReadLine()) != null) { var nameMatch = LogConfig.NameRegex.Match(line); if(nameMatch.Success) { if(current != null) logConfig.Configitems.Add(current); current = new LogConfig.ConfigItem(nameMatch.Groups["value"].Value); continue; } if(current == null) continue; var logLevelMatch = LogConfig.LogLevelRegex.Match(line); if(logLevelMatch.Success) { current.LogLevel = int.Parse(logLevelMatch.Groups["value"].Value); continue; } var filePrintingMatch = LogConfig.FilePrintingRegex.Match(line); if(filePrintingMatch.Success) { current.FilePrinting = bool.Parse(filePrintingMatch.Groups["value"].Value); continue; } var consolePrintingMatch = LogConfig.ConsolePrintingRegex.Match(line); if(consolePrintingMatch.Success) { current.ConsolePrinting = bool.Parse(consolePrintingMatch.Groups["value"].Value); continue; } var screenPrintingMatch = LogConfig.ScreenPrintingRegex.Match(line); if(screenPrintingMatch.Success) { current.ScreenPrinting = bool.Parse(screenPrintingMatch.Groups["value"].Value); continue; } } if(current != null) logConfig.Configitems.Add(current); } } foreach(var requiredLog in requiredLogs) { if(logConfig.Configitems.All(x => x.Name != requiredLog)) { logConfig.Configitems.Add(new LogConfig.ConfigItem(requiredLog)); Logger.WriteLine("Added " + requiredLog + " to log.config.", "UpdateLogConfig"); updated = true; } } if(logConfig.Configitems.Any(x => !x.FilePrinting || x.ConsolePrinting != Config.Instance.LogConfigConsolePrinting)) { foreach(var configItem in logConfig.Configitems) configItem.ResetValues(); updated = true; } if(updated) { using(var sw = new StreamWriter(_logConfigPath)) { foreach(var configItem in logConfig.Configitems) { sw.WriteLine("[{0}]", configItem.Name); sw.WriteLine("LogLevel={0}", configItem.LogLevel); sw.WriteLine("FilePrinting={0}", configItem.FilePrinting.ToString().ToLower()); sw.WriteLine("ConsolePrinting={0}", configItem.ConsolePrinting.ToString().ToLower()); sw.WriteLine("ScreenPrinting={0}", configItem.ScreenPrinting.ToString().ToLower()); } } } } catch(Exception e) { if(_updatedLogConfig) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n Please manually copy the log.config from the Files directory to \"%LocalAppData%/Blizzard/Hearthstone\".", "Error writing log.config"); Application.Current.Shutdown(); } } return updated; }
private static bool UpdateLogConfigFile() { var updated = false; //check for log config and create if not existing try { var requiredLogs = new[] { "Bob", "Power", "Asset", "Rachelle", "Arena", "Achievements", "LoadingScreen" }; var logConfig = new LogConfig(); if (File.Exists(LogConfigPath)) { using (var sr = new StreamReader(LogConfigPath)) { LogConfig.ConfigItem current = null; string line; while (!sr.EndOfStream && (line = sr.ReadLine()) != null) { var nameMatch = LogConfig.NameRegex.Match(line); if (nameMatch.Success) { if (current != null) { logConfig.Configitems.Add(current); } current = new LogConfig.ConfigItem(nameMatch.Groups["value"].Value); continue; } if (current == null) { continue; } var logLevelMatch = LogConfig.LogLevelRegex.Match(line); if (logLevelMatch.Success) { current.LogLevel = int.Parse(logLevelMatch.Groups["value"].Value); continue; } var filePrintingMatch = LogConfig.FilePrintingRegex.Match(line); if (filePrintingMatch.Success) { current.FilePrinting = bool.Parse(filePrintingMatch.Groups["value"].Value); continue; } var consolePrintingMatch = LogConfig.ConsolePrintingRegex.Match(line); if (consolePrintingMatch.Success) { current.ConsolePrinting = bool.Parse(consolePrintingMatch.Groups["value"].Value); continue; } var screenPrintingMatch = LogConfig.ScreenPrintingRegex.Match(line); if (screenPrintingMatch.Success) { current.ScreenPrinting = bool.Parse(screenPrintingMatch.Groups["value"].Value); continue; } var verboseMatch = LogConfig.VerboseRegex.Match(line); if (verboseMatch.Success) { current.Verbose = bool.Parse(verboseMatch.Groups["value"].Value); } } if (current != null) { logConfig.Configitems.Add(current); } } } foreach (var requiredLog in requiredLogs) { if (logConfig.Configitems.All(x => x.Name != requiredLog)) { logConfig.Configitems.Add(new LogConfig.ConfigItem(requiredLog)); Logger.WriteLine("Added " + requiredLog + " to log.config.", "UpdateLogConfig"); updated = true; } } if (logConfig.Configitems.Any(x => !x.FilePrinting || x.ConsolePrinting != Config.Instance.LogConfigConsolePrinting)) { foreach (var configItem in logConfig.Configitems) { configItem.ResetValues(); } updated = true; } if (updated) { using (var sw = new StreamWriter(LogConfigPath)) { foreach (var configItem in logConfig.Configitems) { sw.WriteLine("[{0}]", configItem.Name); sw.WriteLine("LogLevel={0}", configItem.LogLevel); sw.WriteLine("FilePrinting={0}", configItem.FilePrinting.ToString().ToLower()); sw.WriteLine("ConsolePrinting={0}", configItem.ConsolePrinting.ToString().ToLower()); sw.WriteLine("ScreenPrinting={0}", configItem.ScreenPrinting.ToString().ToLower()); sw.WriteLine("Verbose={0}", configItem.Verbose.ToString().ToLower()); } } } } catch (Exception e) { if (Helper.UpdateLogConfig) { MessageBox.Show( e.Message + "\n\n" + e.InnerException + "\n\n Please manually copy the log.config from the Files directory to \"%LocalAppData%/Blizzard/Hearthstone\".", "Error writing log.config"); Application.Current.Shutdown(); } } return(updated); }
private static bool UpdateLogConfigFile() { var updated = false; //check for log config and create if not existing try { var requiredLogs = new[] { "Bob", "Power", "Asset", "Rachelle", "Arena", "Achievements", "LoadingScreen", "Net" }; var logConfig = new LogConfig(); if (File.Exists(LogConfigPath)) { using (var sr = new StreamReader(LogConfigPath)) { LogConfig.ConfigItem current = null; string line; while (!sr.EndOfStream && (line = sr.ReadLine()) != null) { var nameMatch = LogConfig.NameRegex.Match(line); if (nameMatch.Success) { if (current != null) { logConfig.Configitems.Add(current); } current = new LogConfig.ConfigItem(nameMatch.Groups["value"].Value); continue; } if (current == null) { continue; } var logLevelMatch = LogConfig.LogLevelRegex.Match(line); if (logLevelMatch.Success) { current.LogLevel = int.Parse(logLevelMatch.Groups["value"].Value); continue; } var filePrintingMatch = LogConfig.FilePrintingRegex.Match(line); if (filePrintingMatch.Success) { current.FilePrinting = bool.Parse(filePrintingMatch.Groups["value"].Value); continue; } var consolePrintingMatch = LogConfig.ConsolePrintingRegex.Match(line); if (consolePrintingMatch.Success) { current.ConsolePrinting = bool.Parse(consolePrintingMatch.Groups["value"].Value); continue; } var screenPrintingMatch = LogConfig.ScreenPrintingRegex.Match(line); if (screenPrintingMatch.Success) { current.ScreenPrinting = bool.Parse(screenPrintingMatch.Groups["value"].Value); continue; } var verboseMatch = LogConfig.VerboseRegex.Match(line); if (verboseMatch.Success) { current.Verbose = bool.Parse(verboseMatch.Groups["value"].Value); } } if (current != null) { logConfig.Configitems.Add(current); } } } foreach (var requiredLog in requiredLogs) { if (logConfig.Configitems.All(x => x.Name != requiredLog)) { logConfig.Configitems.Add(new LogConfig.ConfigItem(requiredLog)); Log.Info("Added " + requiredLog + " to log.config."); updated = true; } } if (logConfig.Configitems.Any(x => !x.FilePrinting || x.ConsolePrinting != Config.Instance.LogConfigConsolePrinting)) { foreach (var configItem in logConfig.Configitems) { configItem.ResetValues(); } updated = true; } if (updated) { try { // ReSharper disable once ObjectCreationAsStatement new FileInfo(LogConfigPath) { IsReadOnly = false }; } catch (Exception e) { Log.Error("Could not remove read-only from log.config:\n" + e); } using (var sw = new StreamWriter(LogConfigPath)) { foreach (var configItem in logConfig.Configitems) { sw.WriteLine("[{0}]", configItem.Name); sw.WriteLine("LogLevel={0}", configItem.LogLevel); sw.WriteLine("FilePrinting={0}", configItem.FilePrinting.ToString().ToLower()); sw.WriteLine("ConsolePrinting={0}", configItem.ConsolePrinting.ToString().ToLower()); sw.WriteLine("ScreenPrinting={0}", configItem.ScreenPrinting.ToString().ToLower()); sw.WriteLine("Verbose={0}", configItem.Verbose.ToString().ToLower()); } } } } catch (Exception e) { Log.Error(e); throw; } return(updated); }