public Encoding GetEncoding(Stream inStream) { if (_encoding == null) { try { ChoETLLog.Info("Determining file encoding..."); Encoding = ChoFile.GetEncodingFromStream(inStream); ChoETLLog.Info("Found {0} encoding in file.".FormatString(Encoding)); } catch (Exception ex) { Encoding = _defaultEncoding; ChoETLLog.Error("Error finding encoding in file. Default to UTF8."); ChoETLLog.Error(ex.Message); } finally { try { inStream.Position = 0; } catch { } } } return(Encoding); }
public static void SetValue(string key, string value) { ChoGuard.ArgumentNotNullOrEmpty(key, "Key"); if (Configuration == null) { return; } try { if (value == null) { value = String.Empty; } if (Configuration.AppSettings.Settings[key] == null) { Configuration.AppSettings.Settings.Add(key, value); Save(); } else { Configuration.AppSettings.Settings[key].Value = value; Save(); } } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } }
public static string GetValue(string key, string defaultValue = null, bool saveDefaultValue = false) { ChoGuard.ArgumentNotNullOrEmpty(key, "Key"); if (Configuration == null) { return(defaultValue); } try { if (Configuration.AppSettings.Settings[key] == null) { //_appConfig.AppSettings.Settings.Add(key, defaultValue == null ? String.Empty : defaultValue); Configuration.AppSettings.Settings.Add(key, saveDefaultValue ? defaultValue : String.Empty); if (saveDefaultValue) { Save(); } } //else if (_appConfig.AppSettings.Settings[key].Value.IsNullOrEmpty()) //{ // _appConfig.AppSettings.Settings[key].Value = defaultValue == null ? String.Empty : defaultValue; // Save(); //} return(Configuration.AppSettings.Settings[key].Value.IsNullOrWhiteSpace() ? defaultValue : Configuration.AppSettings.Settings[key].Value); } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); return(defaultValue); } }
static ChoAppSettings() { try { if (ChoETLFrxBootstrap.Configuration == null) { if (HttpContext.Current == null) { Configuration = ConfigurationManager.OpenExeConfiguration(null); } else { Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/"); } } else { Configuration = ChoETLFrxBootstrap.Configuration; } } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } }
private static void LoadReferencedAssemblies() { var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList(); var loadedPaths = loadedAssemblies.Select((a) => { if (!a.IsDynamic) { try { return(a.Location); } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } } return(String.Empty); }).ToArray(); var referencedPaths = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll"); var toLoad = referencedPaths.Where(r => !loadedPaths.Contains(r, StringComparer.InvariantCultureIgnoreCase)).ToList(); toLoad.ForEach(path => { try { loadedAssemblies.Add(AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(path))); } catch { } } ); }
private static void Save() { try { Configuation.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } }
public ChoETLSqlServerDbContext(string connectionString) : base(connectionString) { Database.Log = new Action <string>((m) => { if (ChoETLFramework.TraceSwitch.TraceInfo) { Console.WriteLine(m); } ChoETLLog.Info(m); }); Database.SetInitializer <ChoETLSqlServerDbContext <T> >(null); }
private void NotifyRowsLoaded(object sender, ChoRowsLoadedEventArgs e) { EventHandler <ChoRowsLoadedEventArgs> rowsLoadedEvent = RowsLoaded; if (rowsLoadedEvent == null) { ChoETLLog.Info(e.RowsLoaded.ToString("#,##0") + " records loaded."); } else { rowsLoadedEvent(this, e); } }
private void SetDefaultValue(MemberInfo mi, bool saveDefaultValue = false) { object defaultValue = GetDefaultValue(mi.Name); // ChoType.GetRawDefaultValue(mi); try { ChoType.SetMemberValue(this, mi, defaultValue); ChoETLLog.Error("{0}: Assigned default value '{1}' to '{2}' property.".FormatString(NName, defaultValue.ToNString(), mi.Name)); } catch (Exception ex) { ChoETLLog.Error("{0}: Error assigning default value '{1}' to '{2}' property. {3}".FormatString(NName, defaultValue.ToNString(), mi.Name, ex.Message)); } }
public ChoETLSQLiteDbContext(string dbFilePath) : base(new SQLiteConnection() { ConnectionString = new SQLiteConnectionStringBuilder() { DataSource = dbFilePath, ForeignKeys = true }.ConnectionString }, true) { Database.Log = new Action <string>((m) => { //if (ChoETLFramework.TraceSwitch.TraceInfo) // Console.WriteLine(m); ChoETLLog.Info(m); }); Database.SetInitializer <ChoETLSQLiteDbContext <T> >(null); }
public static bool Contains(string key) { ChoGuard.ArgumentNotNullOrEmpty(key, "Key"); try { return(Configuration == null ? false : Configuration.AppSettings.Settings.AllKeys.Contains(key)); } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); return(false); } }
public override void Flush() { while (true) { ChoETLLog.Info("Trace.Flush..."); Push2Queue(TextTraceMsg.FlushTraceMsg); if (_fileWriterThread.Join(1000)) { break; } } base.Flush(); }
public ChoETLSqlServerDbContext(string connectionString) : base(connectionString) { Database.Log = new Action <string>((m) => { if (Log != null) { Log(m); } else { ChoETLLog.Info(m); } }); Database.SetInitializer <ChoETLSqlServerDbContext <T> >(null); }
public ChoETLSQLiteDbContext(string connectionString) : base(new SQLiteConnection() { ConnectionString = connectionString // new SQLiteConnectionStringBuilder() { DataSource = dbFilePath, ForeignKeys = true }.ConnectionString }, true) { Database.Log = new Action <string>((m) => { if (Log != null) { Log(m); } else { ChoETLLog.Info(m); } }); Database.SetInitializer <ChoETLSQLiteDbContext <T> >(null); }
public Encoding GetEncoding(string fileName) { if (_encoding == null) { try { ChoETLLog.Info("Determining '{0}' file encoding...".FormatString(fileName)); Encoding = ChoFile.GetEncodingFromFile(fileName); ChoETLLog.Info("Found '{1}' encoding in '{0}' file.".FormatString(fileName, Encoding)); } catch (Exception ex) { Encoding = _defaultEncoding; ChoETLLog.Error("Error finding encoding in '{0}' file. Default to UTF8.".FormatString(fileName)); ChoETLLog.Error(ex.Message); } } return(Encoding); }
private void Save() { lock (_padLock) { try { if (ChoIniFile.New(this.IniFilePath).GetValue <bool>("IsLocked", false)) { return; } Directory.CreateDirectory(Path.GetDirectoryName(_iniFilePath)); string iniContents = String.Empty; if (File.Exists(_iniFilePath)) { iniContents = File.ReadAllText(_iniFilePath); } if (_sectionName.IsNullOrWhiteSpace()) { iniContents = Regex.Replace(iniContents, @"^(?<ini>[^\[]*)", Matcher, RegexOptions.IgnoreCase); File.WriteAllText(_iniFilePath, iniContents); } else { if (!Regex.IsMatch(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName))) { iniContents += "{0}[{1}]{0}".FormatString(Environment.NewLine, _sectionName); } iniContents = Regex.Replace(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName), Matcher, RegexOptions.IgnoreCase); File.WriteAllText(_iniFilePath, iniContents); } } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } } }
public static Assembly[] LoadAssemblies(string[] directories) { List <Assembly> assemblies = new List <Assembly>(); foreach (string directory in directories) { if (directory == null) { continue; } foreach (string file in Directory.GetFiles(directory, "*.dll;*.exe", SearchOption.AllDirectories)) //TODO: Filter needs to be configurable { if (file == null) { continue; } try { Assembly assembly = Assembly.LoadFile(file); if (assembly != null) { DiscoverNLoadAssemblies(assembly, assemblies); //fileProfile.Info(file); } } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } } } return(assemblies.ToArray()); }
private string GetSection() { lock (_padLock) { try { if (File.Exists(_iniFilePath)) { string iniContents = File.ReadAllText(_iniFilePath); if (_sectionName.IsNullOrWhiteSpace()) { Match m = Regex.Match(iniContents, @"^(?<ini>[^\[]*)"); if (m.Success) { return(m.Groups["ini"].Value); } } else { var match = (from m in Regex.Matches(iniContents, @"^\[[^\]\r\n]+](?:\r?\n(?:[^[\r\n].*)?)*", RegexOptions.Multiline).AsTypedEnumerable <Match>() where m.Value.StartsWith("[{0}]".FormatString(_sectionName), StringComparison.CurrentCultureIgnoreCase) select m).FirstOrDefault(); return(match != null ? match.Value : String.Empty); //Match m = Regex.Match(iniContents, @"(?<ini>\[{0}\][^\[]*)".FormatString(_sectionName), RegexOptions.IgnoreCase); //if (m.Success) // return m.Groups["ini"].Value; } } } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } } return(String.Empty); }
private static Assembly LoadAssemblyFromResource(string name) { //Assembly thisAssembly = Assembly.GetEntryAssembly(); foreach (Assembly thisAssembly in ChoAssembly.GetLoadedAssemblies()) { if (thisAssembly.IsDynamic) { continue; } try { //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name)); if (resources.Count() > 0) { var resourceName = resources.First(); using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName)) { if (stream == null) { return(null); } var block = new byte[stream.Length]; stream.Read(block, 0, block.Length); return(Assembly.Load(block)); } } } catch (Exception ex) { ChoETLLog.Error(ex.ToString()); } } return(null); }
private static void _Initialize() { try { if (ChoETLFrxBootstrap.TraceLevel != null) { TraceLevel = ChoETLFrxBootstrap.TraceLevel.Value; } if (ChoETLFrxBootstrap.Log != null) { Log = ChoETLFrxBootstrap.Log; } if (ChoETLFrxBootstrap.ApplicationName.IsNullOrWhiteSpace()) { ChoETLFrxBootstrap.ApplicationName = ChoPath.EntryAssemblyName; } if (IniFile == null) { IniFile = ChoIniFile.New(ChoPath.EntryAssemblyName); } //#if !DEBUG AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; //#endif AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; if (GetConfigValue <bool>("TurnOnConsoleCtrlHandler", true)) { RegisterConsoleControlHandler(); } #if !NETSTANDARD2_0 try { _elApplicationEventLog = new EventLog("Application", Environment.MachineName, ChoETLFrxBootstrap.ApplicationName); _elApplicationEventLog.Log = "Application"; _elApplicationEventLog.Source = ChoETLFrxBootstrap.ApplicationName; } catch (Exception ex) { WriteLog(ChoETLFramework.TraceSwitch.TraceError, ex.ToString()); } #endif if (_traceLevel == null) { #if DEBUG _switch.Level = GetConfigValue <TraceLevel>("TraceLevel", TraceLevel.Verbose); #else _switch.Level = GetConfigValue <TraceLevel>("TraceLevel", TraceLevel.Off); #endif } else { _switch.Level = GetConfigValue <TraceLevel>("TraceLevel", _traceLevel.Value); } var x = GetConfigValue <string>("LogFileName", ChoETLFrxBootstrap.ApplicationName); if (ChoETLFramework.TraceLevel != TraceLevel.Off) { ChoTextWriterTraceListener frxTextWriterTraceListener = new ChoTextWriterTraceListener("ChoETL", String.Format("BASEFILENAME={0};DIRECTORYNAME={1};FILEEXT={2};TIMESTAMP=false", GetConfigValue <string>("LogFileName", ChoETLFrxBootstrap.LogFileName), GetConfigValue <string>("LogFileDir", ChoETLFrxBootstrap.LogFolder), "log")); if (_frxTextWriterTraceListener != null) { System.Diagnostics.Trace.Listeners.Remove(_frxTextWriterTraceListener); } _frxTextWriterTraceListener = frxTextWriterTraceListener; System.Diagnostics.Trace.Listeners.Add(_frxTextWriterTraceListener); } GlobalProfile = new ChoProfile(ChoETLFramework.TraceSwitch.TraceVerbose, "Time taken to run the application..."); if (ChoAppSettings.Configuration != null) { ChoETLLog.Info("Configuration File Path: " + ChoAppSettings.Configuration.FilePath); } } catch (Exception ex) { if (!ChoETLFrxBootstrap.IsSandboxEnvironment) { Exit(ex); } } finally { } }
private void Initialize() { try { Dictionary <string, object> kvpDict = null; if (_watchChange) { if (_func != null) { kvpDict = _func(); } else { kvpDict = Seed(); } if (kvpDict == null) { return; } Dictionary <string, object> mkvpDict = _kvpDict; bool hasDiff = mkvpDict == null || kvpDict.Except(mkvpDict).Concat(mkvpDict.Except(kvpDict)).Any(); if (!hasDiff) { return; } _kvpDict = kvpDict; } else { kvpDict = _kvpDict; } ERPSPropertyAttribute attr = null; object memberValue = null; string propName = null; //scan through members and load them foreach (var prop in ChoType.GetMembers(GetType()).Where(m => !m.HasAttribute(typeof(ChoIgnoreMemberAttribute)) && !ChoType.IsReadOnlyMember(m))) { attr = ChoType.GetMemberAttribute <ERPSPropertyAttribute>(prop); try { SetDefaultValue(prop, true); propName = attr != null && !attr.Name.IsNullOrWhiteSpace() ? attr.Name : prop.Name; if (kvpDict.ContainsKey(propName)) { memberValue = AfterKVPLoaded(prop.Name, kvpDict[propName]); } else { memberValue = AfterKVPLoaded(prop.Name, null); } if (memberValue != null && memberValue is string) { string mv = memberValue as string; if (attr != null) { switch (attr.TrimOption) { case ChoPropertyValueTrimOption.Trim: mv = mv.Trim(); break; case ChoPropertyValueTrimOption.TrimEnd: mv = mv.TrimEnd(); break; case ChoPropertyValueTrimOption.TrimStart: mv = mv.TrimStart(); break; } } memberValue = mv; } ChoType.ConvertNSetMemberValue(this, prop, memberValue); ChoValidator.ValidateFor(this, prop); } catch (Exception ex) { ChoETLLog.Error("{0}: Error loading '{1}' property. {2}".FormatString(NName, prop.Name, ex.Message)); SetDefaultValue(prop, false); } } } catch (Exception outerEx) { ChoETLLog.Error("{0}: Error loading options. {1}".FormatString(NName, outerEx.Message)); } }