コード例 #1
0
        /// <summary>
        /// ロガーを初期化します。
        /// </summary>
        static Log()
        {
            try
            {
                var thisAsm  = Assembly.GetExecutingAssembly();
                var entryAsm = Assembly.GetEntryAssembly();

                // ロガーを作成する前に、必要なオブジェクトを
                // 読み込んでおきます。
                TargetFactory.AddTargetsFromAssembly(thisAsm, "");
                LayoutFactory.AddLayoutsFromAssembly(thisAsm, "");
                LayoutRendererFactory.AddLayoutRenderersFromAssembly(thisAsm, "");

                // 実行ファイルと同じパスからappname.exe.nlogやNLog.configを検索します。
                var configFileNames = new string[]
                {
                    Path.GetFileName(entryAsm.CodeBase) + ".nlog",
                    "NLog.config",
                };

                var basePath = Path.GetDirectoryName(entryAsm.Location);
                foreach (var configFile in configFileNames)
                {
                    var filename = Path.Combine(basePath, configFile);
                    if (!File.Exists(filename))
                    {
                        continue;
                    }

                    LogManager.Configuration =
                        new XmlLoggingConfiguration(filename);
                    break;
                }

                logger = LogManager.GetCurrentClassLogger();

                // 最後に未処理の例外ハンドラを追加します。
                AppDomain.CurrentDomain.UnhandledException += UnhandledException;
            }
            catch
            {
                // どうしよう。。。
            }
        }
コード例 #2
0
        private void AddExtensionsFromElement(XmlElement element, string baseDirectory)
        {
            if (element == null)
            {
                return;
            }

            foreach (XmlElement targetElement in PropertyHelper.GetChildElements(element))
            {
                if (EqualsCI(targetElement.LocalName, "add"))
                {
                    string assemblyFile = GetCaseInsensitiveAttribute(targetElement, "assemblyFile");
                    string extPrefix    = GetCaseInsensitiveAttribute(targetElement, "prefix");
                    string prefix;
                    if (extPrefix != null && extPrefix.Length != 0)
                    {
                        prefix = extPrefix + ".";
                    }
                    else
                    {
                        prefix = String.Empty;
                    }

                    if (assemblyFile != null && assemblyFile.Length > 0)
                    {
                        try
                        {
                            string fullFileName = Path.Combine(baseDirectory, assemblyFile);
                            InternalLogger.Info("Loading assemblyFile: {0}", fullFileName);
                            Assembly asm = Assembly.LoadFrom(fullFileName);

                            TargetFactory.AddTargetsFromAssembly(asm, prefix);
                            LayoutRendererFactory.AddLayoutRenderersFromAssembly(asm, prefix);
                            FilterFactory.AddFiltersFromAssembly(asm, prefix);
                            LayoutFactory.AddLayoutsFromAssembly(asm, prefix);
                            ConditionMethodFactory.AddConditionMethodsFromAssembly(asm, prefix);
                        }
                        catch (Exception ex)
                        {
                            InternalLogger.Error("Error loading extensions: {0}", ex);
                            if (LogManager.ThrowExceptions)
                            {
                                throw new NLogConfigurationException("Error loading extensions: " + assemblyFile, ex);
                            }
                        }
                        continue;
                    }
                    ;

                    string assemblyName = GetCaseInsensitiveAttribute(targetElement, "assembly");

                    if (assemblyName != null && assemblyName.Length > 0)
                    {
                        try
                        {
                            InternalLogger.Info("Loading assemblyName: {0}", assemblyName);
                            Assembly asm = Assembly.Load(assemblyName);

                            TargetFactory.AddTargetsFromAssembly(asm, prefix);
                            LayoutRendererFactory.AddLayoutRenderersFromAssembly(asm, prefix);
                            FilterFactory.AddFiltersFromAssembly(asm, prefix);
                            LayoutFactory.AddLayoutsFromAssembly(asm, prefix);
                            ConditionMethodFactory.AddConditionMethodsFromAssembly(asm, prefix);
                        }
                        catch (Exception ex)
                        {
                            InternalLogger.Error("Error loading extensions: {0}", ex);
                            if (LogManager.ThrowExceptions)
                            {
                                throw new NLogConfigurationException("Error loading extensions: " + assemblyName, ex);
                            }
                        }
                        continue;
                    }
                    ;
                }
            }
        }
コード例 #3
0
        static int Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage: MakeNLogXSD outputfile.xsd path_to_doc.xml");
                return(1);
            }

            try
            {
                _docXml.Load(args[1]);

                for (int i = 2; i < args.Length; ++i)
                {
                    try
                    {
                        Assembly asm = Assembly.Load(args[i]);
                        TargetFactory.AddTargetsFromAssembly(asm, "");
                        LayoutRendererFactory.AddLayoutRenderersFromAssembly(asm, "");
                        FilterFactory.AddFiltersFromAssembly(asm, "");
                        LayoutFactory.AddLayoutsFromAssembly(asm, "");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("WARNING: {0}", ex.Message);
                    }
                }

                StringWriter sw = new StringWriter();

                sw.Write("<root xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">");

                XmlTextWriter xtw = new XmlTextWriter(sw);
                xtw.Namespaces = false;
                xtw.Formatting = Formatting.Indented;

                _typeDumped[typeof(object)]           = 1;
                _typeDumped[typeof(Target)]           = 1;
                _typeDumped[typeof(TargetWithLayout)] = 1;
                _typeDumped[typeof(TargetWithLayoutHeaderAndFooter)] = 1;
                _typeDumped[typeof(ILayout)] = 1;
                _typeDumped[typeof(ILayoutWithHeaderAndFooter)] = 1;

                foreach (Type targetType in NLog.TargetFactory.TargetTypes)
                {
                    DumpType(xtw, targetType);
                }
                foreach (Type t in FilterFactory.FilterTypes)
                {
                    DumpType(xtw, t);
                }
                foreach (Type t in LayoutFactory.LayoutTypes)
                {
                    DumpType(xtw, t);
                }
                xtw.Flush();
                sw.Write("</root>");
                sw.Flush();

                XmlDocument doc2 = new XmlDocument();
                doc2.LoadXml(sw.ToString());

                using (Stream templateStream = Assembly.GetEntryAssembly().GetManifestResourceStream("MakeNLogXSD.TemplateNLog.xsd"))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(templateStream);

                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
                    nsmgr.AddNamespace("", "http://www.w3.org/2001/XMLSchema");

                    XmlNode n = doc.SelectSingleNode("//types-go-here");

                    foreach (XmlElement el in doc2.DocumentElement.ChildNodes)
                    {
                        XmlNode importedNode = doc.ImportNode(el, true);
                        n.ParentNode.InsertBefore(importedNode, n);
                    }
                    n.ParentNode.RemoveChild(n);

                    n = doc.SelectSingleNode("//filters-go-here");
                    foreach (Type t in FilterFactory.FilterTypes)
                    {
                        FilterAttribute fa = (FilterAttribute)Attribute.GetCustomAttribute(t, typeof(FilterAttribute));
                        XmlElement      el = doc.CreateElement("xs:element", XmlSchema.Namespace);
                        el.SetAttribute("name", fa.Name);
                        el.SetAttribute("type", SimpleTypeName(t));
                        n.ParentNode.InsertBefore(el, n);
                    }
                    n.ParentNode.RemoveChild(n);

                    Console.WriteLine("Saving schema to: {0}", args[0]);
                    doc.Save(args[0]);
                    return(0);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex);
                return(1);
            }
        }