public static string GetConfigFilePath() { if (!_configFilePath.IsNullOrWhiteSpace()) { return(_configFilePath); } else { string configFilePath; string appFrxFilePath = ChoAppFrxSettings.Me.AppFrxFilePath; if (appFrxFilePath.IsNullOrWhiteSpace()) { configFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, ChoReservedFileName.CoreFrxConfigFileName)); } else { appFrxFilePath = ChoString.ExpandProperties(appFrxFilePath, ChoEnvironmentVariablePropertyReplacer.Instance); if (ChoPath.IsDirectory(appFrxFilePath)) { appFrxFilePath = Path.Combine(appFrxFilePath, ChoReservedFileName.CoreFrxConfigFileName); } configFilePath = appFrxFilePath; } return(configFilePath); } }
public static string ExpandProperties(object target, string inString) { if (String.IsNullOrEmpty(inString)) { return(inString); } return(ChoString.ExpandProperties(target, inString)); }
// If the type of the value to convert is string, parses the string // and returns the integer to set the value of the property to. // This example first extends the integer array that supplies the // standard values collection if the user-entered value is not // already in the array. public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value != null && value.GetType() == typeof(string)) { return(ChoString.ExpandProperties(null, value as string)); } //return ChoString.Evaluate(null, value as string); return(value); }
public bool Format(object target, ref string msg) { try { if (msg.IndexOf("{") == -1) { return(true); } msg = ChoString.ExpandProperties(target, msg, '{', '}', '^', new ChoCustomKeyValuePropertyReplacer(target)); return(true); } catch (ChoFatalApplicationException) { throw; } catch (Exception ex) { msg = ChoPropertyManager.FormatException(msg, ex); return(false); } }
private void LoadXml(XmlDocument doc) { if (doc == null) { return; } XmlNode rootNode = doc.SelectSingleNode("//sharedEnvironment"); if (rootNode != null) { if (rootNode.Attributes["baseAppSharedConfigDirectory"] != null) { BaseAppConfigDirectory = ChoPath.GetFullPath(ChoString.ExpandProperties(rootNode.Attributes["baseAppSharedConfigDirectory"].Value, ChoEnvironmentVariablePropertyReplacer.Instance)); } if (BaseAppConfigDirectory.IsNullOrWhiteSpace()) { BaseAppConfigDirectory = ChoPath.AssemblyBaseDirectory; } if (rootNode.Attributes["defaultEnvironment"] != null) { DefaultEnvironment = rootNode.Attributes["defaultEnvironment"].Value; } XmlNodeList envNodes = rootNode.SelectNodes("environment"); if (envNodes != null) { List <ChoEnvironmentDetails> environmentDetailList = new List <ChoEnvironmentDetails>(); foreach (XmlNode envNode in envNodes) { ChoEnvironmentDetails environmentDetails = new ChoEnvironmentDetails(); if (envNode.Attributes["name"] != null) { environmentDetails.Name = envNode.Attributes["name"].Value; if (!environmentDetails.Name.IsNullOrWhiteSpace()) { if (envNode.Attributes["freeze"] != null) { Boolean.TryParse(envNode.Attributes["freeze"].Value, out environmentDetails.Freeze); } if (envNode.Attributes["appFrxFilePath"] != null) { environmentDetails.AppFrxFilePath = envNode.Attributes["appFrxFilePath"].Value; if (!environmentDetails.AppFrxFilePath.IsNullOrWhiteSpace()) { if (!Path.IsPathRooted(environmentDetails.AppFrxFilePath)) { environmentDetails.AppFrxFilePath = ChoString.ExpandProperties(environmentDetails.AppFrxFilePath, ChoEnvironmentVariablePropertyReplacer.Instance); if (!Path.IsPathRooted(environmentDetails.AppFrxFilePath)) { environmentDetails.AppFrxFilePath = Path.Combine(BaseAppConfigDirectory, environmentDetails.AppFrxFilePath); } if (ChoPath.IsDirectory(environmentDetails.AppFrxFilePath)) { environmentDetails.AppFrxFilePath = Path.Combine(environmentDetails.AppFrxFilePath, ChoReservedFileName.CoreFrxConfigFileName); } } } else { environmentDetails.AppFrxFilePath = Path.Combine(BaseAppConfigDirectory, environmentDetails.Name, ChoReservedFileName.CoreFrxConfigFileName); } } else { environmentDetails.AppFrxFilePath = Path.Combine(BaseAppConfigDirectory, environmentDetails.Name, ChoReservedFileName.CoreFrxConfigFileName); } XmlNodeList machineNodes = envNode.SelectNodes("machine"); if (machineNodes != null) { List <string> machines = new List <string>(); foreach (XmlNode machineNode in machineNodes) { machines.Add(machineNode.InnerText); } environmentDetails.Machines = machines.ToArray(); } environmentDetailList.Add(environmentDetails); } } } EnvironmentDetails = environmentDetailList.ToArray(); foreach (ChoEnvironmentDetails environmentDetail in EnvironmentDetails) { environmentDetail.Parent = this; } } } }
private static string ToString(object target, bool collectErrMsgs, ChoMemberFormatterAttribute memberFormaterAttribute1, ChoMemberItemFormatterAttribute memberItemFormaterAttribute1, BindingFlags bindingFlags) { if (target == null) { return(String.Empty); } if (memberFormaterAttribute1 != null && memberFormaterAttribute1.CanFormat()) { if (target.GetType().IsSimple()) { return(memberFormaterAttribute1.FormatObject(target)); } else { ChoStringMsgBuilder msg = new ChoStringMsgBuilder(); //msg.AppendLineIfNoNL(memberFormaterAttribute1.FormatObject(target)); msg.Append(memberFormaterAttribute1.FormatObject(target)); //msg.AppendNewLine(); if (collectErrMsgs) { GetErrorMsgs(target, msg); } return(msg.ToString()); } } else if (target.GetType().IsSimple() || target.GetType().IsMethodImplemented("ToString")) { return(target.ToString()); } else if (target is Delegate) { return(String.Format("{0}.{1}", ((Delegate)target).Target == null ? "[Static]" : ((Delegate)target).Target.GetType().FullName, ((Delegate)target).Method.Name)); } else if (target is IEnumerable) { StringBuilder arrMsg = new StringBuilder(); int count = 0; foreach (object item in (IEnumerable)target) { count++; arrMsg.AppendFormat("{0}{1}", ToString(item, collectErrMsgs, memberItemFormaterAttribute1, null, bindingFlags), Environment.NewLine); } return("[Count: {0}]{1}{2}".FormatString(count, Environment.NewLine, arrMsg.ToString())); } else { bool foundMatchingFormatter = false; string retValue = ChoObject.Format(target, null, out foundMatchingFormatter); if (foundMatchingFormatter) { if (target.GetType().IsSimple()) { return(retValue); } else { ChoStringMsgBuilder msg = new ChoStringMsgBuilder(); msg.AppendFormat(retValue); msg.AppendNewLine(); if (collectErrMsgs) { GetErrorMsgs(target, msg); } return(msg.ToString()); } } else { ChoInterceptableObject interceptableObject = null; try { if (ChoType.IsRealProxyObject(target.GetType())) { interceptableObject = ChoInterceptableObject.Silentable(target as ChoInterceptableObject); } ChoTypeFormatterAttribute objectFormatter = ChoType.GetAttribute(target.GetType(), typeof(ChoTypeFormatterAttribute)) as ChoTypeFormatterAttribute; if (objectFormatter == null) { ChoStringMsgBuilder msg = new ChoStringMsgBuilder(memberFormaterAttribute1 == null ? String.Format("{0} State", target.GetType().FullName) : memberFormaterAttribute1.Name); //MemberInfo[] memberInfos = target.GetType().GetMembers(bindingFlags /*BindingFlags.Public | BindingFlags.Instance /*| BindingFlags.DeclaredOnly*/ /*| BindingFlags.GetField | BindingFlags.GetProperty*/); IEnumerable <MemberInfo> memberInfos = ChoType.GetGetFieldsNProperties(target.GetType(), bindingFlags); if (memberInfos == null || memberInfos.Count() == 0) { msg.AppendFormatLine(ChoStringMsgBuilder.Empty); } else { foreach (MemberInfo memberInfo in memberInfos) { if (!ChoType.IsValidObjectMember(memberInfo)) { continue; } ChoIgnoreMemberFormatterAttribute memberFormatterIgnoreAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoIgnoreMemberFormatterAttribute)) as ChoIgnoreMemberFormatterAttribute; if (memberFormatterIgnoreAttribute != null) { continue; } string memberText = GetNFormatMemberValue(target, memberInfo, collectErrMsgs, null, null, bindingFlags); int count = 0; if (GetCountFromMsg(ref memberText, ref count)) { msg.AppendFormatLine("{0} [Length: {2}]: {1}", memberInfo.Name, memberText, count); } else if (!memberText.ContainsHeader()) { msg.AppendFormatLine("{0}: {1}", memberInfo.Name, memberText); } else { msg.AppendFormatLine("{0}: {1}", memberInfo.Name, memberText); } } } msg.AppendNewLine(); if (collectErrMsgs) { GetErrorMsgs(target, msg); } return(msg.ToString()); } else { ChoStringMsgBuilder msg = new ChoStringMsgBuilder(ChoString.ExpandProperties(target, objectFormatter.Header));; if (objectFormatter.HasFormatSpecified) { msg.AppendFormat(objectFormatter.FormatObject(target)); } else { //MemberInfo[] memberInfos = target.GetType().GetMembers(bindingFlags /*BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance /*| BindingFlags.DeclaredOnly*/ /*| BindingFlags.GetField | BindingFlags.GetProperty*/); IEnumerable <MemberInfo> memberInfos = ChoType.GetGetFieldsNProperties(target.GetType(), bindingFlags); if (memberInfos == null || memberInfos.Count() == 0) { msg.AppendFormatLine(ChoStringMsgBuilder.Empty); } else { bool isPublicMember = true; foreach (MemberInfo memberInfo in memberInfos) { if (!ChoType.IsValidObjectMember(memberInfo)) { continue; } isPublicMember = true; ChoIgnoreMemberFormatterAttribute memberFormatterIgnoreAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoIgnoreMemberFormatterAttribute)) as ChoIgnoreMemberFormatterAttribute; if (memberFormatterIgnoreAttribute != null) { continue; } ChoMemberFormatterAttribute memberFormaterAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoMemberFormatterAttribute)) as ChoMemberFormatterAttribute; ChoMemberItemFormatterAttribute memberItemFormaterAttribute = ChoType.GetMemberAttribute(memberInfo, typeof(ChoMemberItemFormatterAttribute)) as ChoMemberItemFormatterAttribute; if (memberInfo is PropertyInfo) { MethodInfo getMethod = ((PropertyInfo)memberInfo).GetGetMethod(true); isPublicMember = getMethod != null && getMethod.IsPublic; } else if (memberInfo is FieldInfo) { isPublicMember = ((FieldInfo)memberInfo).IsPublic; } else { continue; } if (isPublicMember || (!isPublicMember && memberFormaterAttribute != null)) { object memberValue = ChoType.GetMemberValue(target, memberInfo); if (memberValue == target) { return(null); } string memberText = GetNFormatMemberValue(target, memberInfo, collectErrMsgs, memberFormaterAttribute, memberItemFormaterAttribute, bindingFlags); string memberName = memberFormaterAttribute == null || memberFormaterAttribute.Name.IsNullOrEmpty() ? memberInfo.Name : ChoPropertyManager.ExpandProperties(target, memberFormaterAttribute.Name); if (memberFormaterAttribute == null || !memberFormaterAttribute.CanFormat()) { int count = 0; if (GetCountFromMsg(ref memberText, ref count)) { msg.AppendFormatLine("{0} [Length: {2}]: {1}", memberName, memberText, count); } else if (memberText.ContainsHeader()) { msg.AppendFormatLine("{0}: {1}", memberName, memberText); } else { msg.AppendFormatLine("{0}: {1}", memberName, memberText); } } else if (memberFormaterAttribute.Name == ChoNull.NullString) { msg.Append(memberFormaterAttribute.PostFormat(memberName, memberText)); } else { msg.AppendFormat(memberFormaterAttribute.PostFormat(memberName, memberText)); } } } } } //msg.AppendNewLine(); if (collectErrMsgs) { GetErrorMsgs(target, msg); } return(msg.ToString()); } } finally { if (interceptableObject != null) { interceptableObject.Dispose(); } } } } }
public void Initialize() { if (ApplicationBehaviourSettings == null) { ApplicationBehaviourSettings = new ChoApplicationBehaviourSettings(); } if (TrayApplicationBehaviourSettings == null) { TrayApplicationBehaviourSettings = new ChoTrayApplicationBehaviourSettings(); } if (LogSettings == null) { LogSettings = new ChoLogSettings(); } if (ApplicationName.IsNullOrWhiteSpace()) { try { ApplicationName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } catch (System.Security.SecurityException ex) { ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString()); } } if (ApplicationName.IsNullOrEmpty()) { ChoApplication.OnFatalApplicationException(101, "Missing ApplicationName."); } ApplicationNameWithoutExtension = Path.GetFileNameWithoutExtension(ApplicationName); if (EventLogSourceName.IsNullOrWhiteSpace()) { EventLogSourceName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } if (LogSettings.LogTimeStampFormat.IsNullOrWhiteSpace()) { LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } if (LogSettings.LogFileName.IsNullOrWhiteSpace()) { LogSettings.LogFileName = ChoPath.ChangeExtension(ApplicationName, ChoReservedFileExt.Log); } LogSettings.LogFileName = ChoPath.CleanFileName(LogSettings.LogFileName); if (Path.IsPathRooted(LogSettings.LogFileName)) { LogSettings.LogFileName = Path.GetFileName(LogSettings.LogFileName); } try { DateTime.Now.ToString(LogSettings.LogTimeStampFormat); } catch (Exception ex) { ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, "Invalid LogTimeStampFormat '{0}' configured.".FormatString(LogSettings.LogTimeStampFormat)); ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString()); LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } try { string sharedEnvConfigDir = null; if (!AppFrxConfigFilePath.IsNullOrEmpty()) { sharedEnvConfigDir = Path.GetDirectoryName(AppFrxConfigFilePath); } if (AppConfigFilePath.IsNullOrWhiteSpace()) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml))); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml)); } } else { if (!Path.IsPathRooted(AppConfigFilePath)) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, AppConfigFilePath)); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, AppConfigFilePath); } } else { ApplicationConfigFilePath = AppFrxConfigFilePath; } } ApplicationConfigDirectory = Path.GetDirectoryName(ApplicationConfigFilePath); } catch (System.Security.SecurityException ex) { // This security exception will occur if the caller does not have // some undefined set of SecurityPermission flags. ChoApplication.Trace(ChoTrace.ChoSwitch.TraceError, ex.ToString()); } #region Get HostName // Get the DNS host name of the current machine try { // Lookup the host name if (HostName.IsNullOrWhiteSpace()) { HostName = System.Net.Dns.GetHostName(); } } catch (System.Net.Sockets.SocketException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the hostname // You must have Unrestricted DnsPermission to access resource } // Get the NETBIOS machine name of the current machine if (HostName.IsNullOrWhiteSpace()) { try { HostName = Environment.MachineName; } catch (InvalidOperationException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the machine name // You must have Unrestricted EnvironmentPermission to access resource } } #endregion Get HostName #region Get IpAddresses try { IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); for (int i = 0; i < localIPs.Length; i++) { IPAddresses.Add(localIPs[i].ToString()); } } catch (System.Net.Sockets.SocketException) { } catch (System.Security.SecurityException) { // We may get a security exception looking up the hostname // You must have Unrestricted DnsPermission to access resource } #endregion Get IpAddresses if (LogSettings.TraceLevel < 0) { LogSettings.TraceLevel = 4; } if (!LogSettings.LogFolder.IsNullOrWhiteSpace()) { ApplicationLogDirectory = ChoString.ExpandProperties(LogSettings.LogFolder, ChoEnvironmentVariablePropertyReplacer.Instance); } //else // ApplicationLogDirectory = Path.Combine(Path.GetDirectoryName(ChoGlobalApplicationSettings.SharedEnvConfigPath), ChoReservedDirectoryName.Logs); if (ApplicationLogDirectory.IsNullOrWhiteSpace()) { if (ChoApplication.AppEnvironment.IsNullOrWhiteSpace()) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs); } else { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs, ChoApplication.AppEnvironment); } } if (!Path.IsPathRooted(ApplicationLogDirectory)) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ApplicationLogDirectory); } if (ChoApplication.ApplicationMode == ChoApplicationMode.Service) { TrayApplicationBehaviourSettings.TurnOn = false; } }
public static object Evaluate(object target, string msg) { //return ChoString.Evaluate(target, msg); return(ChoString.ExpandProperties(target, msg)); }
internal void PostInitialize() { if (ApplicationName.IsNullOrEmpty()) { ChoEnvironment.Exit(101, "Missing ApplicationName."); } if (EventLogSourceName.IsNullOrWhiteSpace()) { EventLogSourceName = System.IO.Path.GetFileName(ChoAssembly.GetEntryAssemblyLocation()); } if (LogSettings.LogTimeStampFormat.IsNullOrWhiteSpace()) { LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } if (LogSettings.LogFileName.IsNullOrWhiteSpace()) { LogSettings.LogFileName = ChoPath.ChangeExtension(ApplicationName, ChoReservedFileExt.Log); } LogSettings.LogFileName = ChoPath.CleanFileName(LogSettings.LogFileName); if (Path.IsPathRooted(LogSettings.LogFileName)) { LogSettings.LogFileName = Path.GetFileName(LogSettings.LogFileName); } try { DateTime.Now.ToString(LogSettings.LogTimeStampFormat); } catch (Exception ex) { ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, "Invalid LogTimeStampFormat '{0}' configured.".FormatString(LogSettings.LogTimeStampFormat)); ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, ex.ToString()); LogSettings.LogTimeStampFormat = "yyyy-MM-dd hh:mm:ss.fffffff"; } try { string sharedEnvConfigDir = null; if (!AppFrxConfigFilePath.IsNullOrEmpty()) { sharedEnvConfigDir = Path.GetDirectoryName(ChoPath.GetFullPath(AppFrxConfigFilePath)); } if (AppConfigFilePath.IsNullOrWhiteSpace()) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml))); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, ChoPath.AddExtension(ChoPath.CleanFileName(ApplicationName), ChoReservedFileExt.Xml)); } } else { if (!Path.IsPathRooted(AppConfigFilePath)) { if (sharedEnvConfigDir.IsNullOrWhiteSpace()) { ApplicationConfigFilePath = ChoPath.GetFullPath(Path.Combine(ChoReservedDirectoryName.Config, AppConfigFilePath)); } else { ApplicationConfigFilePath = Path.Combine(sharedEnvConfigDir, AppConfigFilePath); } } else { ApplicationConfigFilePath = AppConfigFilePath; } } ApplicationConfigDirectory = Path.GetDirectoryName(ChoPath.GetFullPath(ApplicationConfigFilePath)); AppFrxConfigFilePath = Path.Combine(ApplicationConfigDirectory, ChoReservedFileName.CoreFrxConfigFileName); } catch (System.Security.SecurityException ex) { // This security exception will occur if the caller does not have // some undefined set of SecurityPermission flags. ChoApplication.Trace(ChoTraceSwitch.Switch.TraceError, ex.ToString()); } if (!LogSettings.LogFolder.IsNullOrWhiteSpace()) { ApplicationLogDirectory = ChoString.ExpandProperties(LogSettings.LogFolder, ChoEnvironmentVariablePropertyReplacer.Instance); } if (ApplicationLogDirectory.IsNullOrWhiteSpace()) { if (ChoApplication.AppEnvironment.IsNullOrWhiteSpace()) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs); } else { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ChoReservedDirectoryName.Logs, ChoApplication.AppEnvironment); } } if (!Path.IsPathRooted(ApplicationLogDirectory)) { ApplicationLogDirectory = Path.Combine(ChoPath.AssemblyBaseDirectory, ApplicationLogDirectory); } if (ChoApplication.ApplicationMode == ChoApplicationMode.Service) { TrayApplicationBehaviourSettings.TurnOn = false; } }