Esempio n. 1
0
        public static void CreateSubLog(string moduleName, string baseModuleName)
        {
            try
            {
                FieldInfo fi = typeof(TraceDispenser).GetField("g_ListenerList", BindingFlags.Static | BindingFlags.NonPublic);
                TraceDispenser td = new TraceDispenser();
                string key = string.Format("\\{0}\\{1}.{2}", Constants.APPLICATION_NAME, baseModuleName, moduleName);
                Hashtable tracelist = (Hashtable)fi.GetValue(td);
                if (!tracelist.ContainsKey(key))
                {
                    TraceLog baseLog = TraceDispenser.GetTraceLog(baseModuleName, Constants.APPLICATION_NAME);
                    ArrayList baseconfigs = (ArrayList)GetFieldValue(baseLog, "TracerConfigList");
                    TraceLog newlog = new TraceLog();

                    Type btftype = typeof(TraceLog).Assembly.GetType("Com.SouthernCompany.Cool.Diagnostics.BaseTracerFile");
                    object btf = Activator.CreateInstance(btftype);

                    //Path.GetInvalidFileNameChars()
                    string safemodulename = moduleName;
                    foreach (char c in Path.GetInvalidFileNameChars())
                        safemodulename = safemodulename.Replace(c.ToString(), "");
                    string basefilename = (string)GetFieldValue(baseconfigs[0], "m_FileName");
                    SetFieldValue(btf, "m_FileName", Path.Combine(Path.GetDirectoryName(basefilename), baseModuleName + "." + safemodulename + ".log"));
                    SetFieldValue(btf, "m_PrevFileName", Path.Combine(Path.GetDirectoryName(basefilename), baseModuleName + "." + safemodulename + ".prev.log"));
                    SetFieldValue(btf, "m_EnableLogging", GetFieldValue(baseconfigs[0], "m_EnableLogging"));
                    SetFieldValue(btf, "m_EnableFlush", GetFieldValue(baseconfigs[0], "m_EnableFlush"));
                    SetFieldValue(btf, "m_ActiveFilterMask", GetFieldValue(baseconfigs[0], "m_ActiveFilterMask"));
                    SetFieldValue(btf, "m_MaxLogFileSize", GetFieldValue(baseconfigs[0], "m_MaxLogFileSize"));

                    SetFieldValue(newlog, "TracerConfigList", new ArrayList(new object[] { btf }));

                    MethodInfo mi = typeof(TraceLog).GetMethod("SetAppName", BindingFlags.NonPublic | BindingFlags.Instance);
                    mi.Invoke(newlog, new object[] { baseModuleName + "." + moduleName, Constants.APPLICATION_NAME, "" });

                    mi = typeof(TraceLog).GetMethod("InitializeMutex", BindingFlags.NonPublic | BindingFlags.Instance);
                    mi.Invoke(newlog, new object[0]);//, new object[] { moduleName, Constants.APPLICATION_NAME, "" });

                    mi = typeof(TraceLog).GetMethod("InitializeLogging", BindingFlags.NonPublic | BindingFlags.Instance);
                    mi.Invoke(newlog, new object[0]);// new object[] { moduleName, Constants.APPLICATION_NAME, "" });
                    newlog.LogEvent("CoolTracer version=" + btftype.Assembly.ToString(), (long)0x10L);
                    newlog.LogEvent("Identity=" + WindowsIdentity.GetCurrent().Name.ToString(), (long)0x10L);

                    tracelist.Add(key, newlog);
                    Log.Write(string.Format("Created sub-log '{0}.{1}', filename '{2}'.", baseModuleName, moduleName, Path.Combine(Path.GetDirectoryName(basefilename), baseModuleName + "." + safemodulename + ".log")), Mask.Diagnostic);
                }
            }
            catch (Exception ex)
            {
                Log.Write(string.Format("Unable to create sub-log '{0}.{1}': ", baseModuleName, moduleName) + ex.ToString(), Mask.Failure);
            }
        }