Esempio n. 1
0
 public UseCaseTests()
 {
     Bilge.AddMessageHandler(new TCPHandler("127.0.0.1", 9060));
     Bilge.SetConfigurationResolver((a, b) => {
         return(System.Diagnostics.SourceLevels.Verbose);
     });
 }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            string            dt  = $"{DateTime.Now.Hour}{DateTime.Now.Minute}{DateTime.Now.Second}";
            FileSystemHandler fsh = new FileSystemHandler(new FSHandlerOptions("c:\\Temp\\votalot\\dummy" + dt + ".log"));

            fsh.SetFormatter(new FlimFlamV2Formatter());
            Bilge.AddMessageHandler(new ConsoleHandler());
            Bilge.AddMessageHandler(new TCPHandler("127.0.0.1", 9060));
            Bilge.AddMessageHandler(fsh);
            Bilge.Alert.Online("TestClient");

#if CORE
            // Due to the dependency on configurations and the different ways that you can configure a core service this is not
            // implemented within Bilge even as a default but is documented instead.
            Bilge.SetConfigurationResolver((s, lvl) => {
                var configuration = new ConfigurationBuilder()
                                    .AddJsonFile("appsettings.json", false, true)
                                    .Build();

                SourceLevels result            = lvl;
                string defaultValue            = configuration["logging:loglevel:default"];
                string specificForThisInstance = configuration[$"logging:loglevel:{s}"];

                if (Enum.TryParse <SourceLevels>(specificForThisInstance, out SourceLevels slSpecific))
                {
                    result = slSpecific;
                }
                else
                {
                    if (Enum.TryParse <SourceLevels>(defaultValue, out SourceLevels slDefault))
                    {
                        result = slDefault;
                    }
                }

                return(result);
            });
#else
            Bilge.SetConfigurationResolver((instanceName, lvl) => {
                // Logic -> Try Source Switch, Failing that Trace Switch, failing that SourceSwitch + Switch, Failing that TraceSwitch+Switch.

                SourceLevels result   = lvl;
                bool tryTraceSwitches = true;

                try {
                    SourceSwitch ss = new SourceSwitch(instanceName);
                    if (ss.Level == SourceLevels.Off)
                    {
                        ss = new SourceSwitch($"{instanceName}Switch");
                        if (ss.Level == SourceLevels.Off)
                        {
                        }
                        else
                        {
                            tryTraceSwitches = false;
                            result           = ss.Level;
                        }
                    }
                    else
                    {
                        tryTraceSwitches = false;
                        result           = ss.Level;
                    }
                } catch (SystemException) {
                    // This is the higher level exception of a ConfigurationErrorsException but that one requires a separate reference
                    // This occurs when a TraceSwitch has the same name as the source switch with a value that is not supported by source switch e.g. Info
                }

                if (tryTraceSwitches)
                {
                    TraceSwitch ts = new TraceSwitch(instanceName, "");
                    if (ts.Level == TraceLevel.Off)
                    {
                        ts = new TraceSwitch($"{instanceName}Switch", "");
                        if (ts.Level != TraceLevel.Off)
                        {
                            result = Bilge.ConvertTraceLevel(ts.Level);
                        }
                    }
                    else
                    {
                        result = Bilge.ConvertTraceLevel(ts.Level);
                    }
                }

                return(result);
            });
#endif

            Bilge b = new Bilge("PliskyConsoleTestApp");
            b.ActiveTraceLevel = SourceLevels.Verbose;

            b.Verbose.Log("Hello Cruel World");

            bool traceSwitchTests   = false;
            bool bulkFileWriteTests = false;
            bool perfTests          = false;
            bool basicWriteAllTest  = true;
            bool actionTest         = true;

            if (actionTest)
            {
                int acCount = 0;


                b.Action.RegisterHandler((m) => {
                    if (m.Success)
                    {
                        acCount++;
                    }
                }, "bob");

                b.Action.Occured("Event", "Data");


                if (acCount == 0)
                {
                    throw new InvalidOperationException("didnt work");
                }
            }


            if (basicWriteAllTest)
            {
                Bilge.SetConfigurationResolver((instanceName, lvl) => {
                    return(SourceLevels.Verbose);
                });

                ModularWriting mw = new ModularWriting();
                mw.DoWrite();
            }
            if (bulkFileWriteTests)
            {
                ProductionLoggingTest(b);
            }
            b.Flush();

            if (perfTests)
            {
                PerformanceTest p = new PerformanceTest();
                p.AnalysisBatchVsNoBatch();
                //p.ExecuteTest();
                p.WriteOutput();

                Bilge.ForceFlush();
            }
            Console.ReadLine();
            return;

            b.AddHandler(new SimpleTraceFileHandler(@"c:\temp\"));
            if (traceSwitchTests)
            {
                Console.WriteLine("Actual Trace Level : " + b.ActiveTraceLevel.ToString());

                try {
                    TraceSource ts = new TraceSource("monkeySwitch", SourceLevels.Off);
                    TraceSwitch tx = new TraceSwitch("monkey2Switch", "test", "Off");

                    SourceSwitch sw = new SourceSwitch("boner", "off");

                    Console.WriteLine($"{ts.Switch.Level} >> {tx.Level} >> {sw.Level}");

                    Console.ReadLine();
                } catch (Exception ex) {
                }
            }

            bool doDirectWriting = false;

            if (args.Length > 0)
            {
                if (args[0] == "direct")
                {
                    doDirectWriting = true;
                }
            }

            if (doDirectWriting)
            {
                var dr = new DirectWriting(b);
                dr.DoDirectWrites();
            }

            ModularWriting mr = new ModularWriting();
            mr.DoWrite();
            b.Flush();
            Console.WriteLine("Readline");
            Console.ReadLine();
        }