Exemple #1
0
        static void Main(string[] args)
        {
            LayoutRendererFactory.AddLayoutRenderer("AppSetting", typeof(AppSettingLayoutReader));

            LogFactory logFactory = new LogFactory(new XmlLoggingConfiguration("c:\\NLog.config"));

            //TargetCollection targetCollection = LogManager.Configuration.GetConfiguredNamedTargets();
            //int cant = targetCollection.Count;
            //Target target = targetCollection[0];

            //if (target is FileTarget)
            //{
            //    string nombreArchivo = (target as FileTarget).FileName;
            //    Console.WriteLine(nombreArchivo);
            //}

            Logger logger1 = logFactory.GetLogger("Logger1");
            Logger logger2 = logFactory.GetLogger("Logger2");

            for (int i = 0; i < 1000; i++)
            {
                logger1.Debug("Archivo de salida 1: " + i.ToString("000"));
                logger2.Debug("Archivo de salida 2: " + i.ToString("000"));
            }

            Console.WriteLine("Press any key to contine");
            Console.ReadLine();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationItemFactory"/> class.
        /// </summary>
        /// <param name="assemblies">The assemblies to scan for named items.</param>
        public ConfigurationItemFactory(params Assembly[] assemblies)
        {
            this.CreateInstance = FactoryHelper.CreateInstance;
            this.targets = new Factory<Target, TargetAttribute>(this);
            this.filters = new Factory<Filter, FilterAttribute>(this);
            this.layoutRenderers = new LayoutRendererFactory(this);
            this.layouts = new Factory<Layout, LayoutAttribute>(this);
            this.conditionMethods = new MethodFactory<ConditionMethodsAttribute, ConditionMethodAttribute>();
            this.ambientProperties = new Factory<LayoutRenderer, AmbientPropertyAttribute>(this);
            this.timeSources = new Factory<TimeSource, TimeSourceAttribute>(this);
            this.allFactories = new List<object>
            {
                this.targets,
                this.filters,
                this.layoutRenderers,
                this.layouts,
                this.conditionMethods,
                this.ambientProperties,
                this.timeSources,
            };

            foreach (var asm in assemblies)
            {
                this.RegisterItemsFromAssembly(asm);
            }
        }
Exemple #3
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            // Setup our custom controller factory so that the [HandleErrorWithElmah] attribute
            // is automatically injected into all of the controllers
            ControllerBuilder.Current.SetControllerFactory(new ErrorHandlingControllerFactory());

            // Register custom NLog Layout renderers
            LayoutRendererFactory.AddLayoutRenderer("utc_date", typeof(MvcLoggingDemo.Services.Logging.NLog.UtcDateRenderer));
            LayoutRendererFactory.AddLayoutRenderer("web_variables", typeof(MvcLoggingDemo.Services.Logging.NLog.WebVariablesRenderer));
        }
Exemple #4
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
            {
                // どうしよう。。。
            }
        }
Exemple #5
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);
            }
        }