|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Reflection; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Xml; |
| 9 | +using log4net.Config; |
| 10 | +using log4net.Repository; |
| 11 | +using Microsoft.Extensions.Configuration; |
| 12 | +using NLog; |
| 13 | +using NLog.Config; |
| 14 | +using StackifyLib; |
| 15 | + |
| 16 | +namespace CoreConsoleApp |
| 17 | +{ |
| 18 | + public class Program |
| 19 | + { |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + static void Main(string[] args) |
| 24 | + { |
| 25 | + var builder = new ConfigurationBuilder() |
| 26 | + .SetBasePath(Directory.GetCurrentDirectory()) |
| 27 | + .AddJsonFile("appsettings.json", optional: true, |
| 28 | + reloadOnChange: true); |
| 29 | + |
| 30 | + var config = builder.Build(); |
| 31 | + config.ConfigureStackifyLogging(); |
| 32 | + // OR |
| 33 | + //StackifyLib.Config.SetConfiguration(config); |
| 34 | + |
| 35 | + //enable debug logging |
| 36 | + StackifyLib.Utils.StackifyAPILogger.OnLogMessage += StackifyAPILogger_OnLogMessage; |
| 37 | + StackifyLib.Utils.StackifyAPILogger.LogEnabled = true; |
| 38 | + |
| 39 | + |
| 40 | + NLogTest(); |
| 41 | + |
| 42 | + StackifyLib.Logger.Shutdown(); //best practice for console apps |
| 43 | + } |
| 44 | + |
| 45 | + private static void NLogTest() |
| 46 | + { |
| 47 | + var path = Path.Combine(Directory.GetCurrentDirectory(), "nlog.config"); |
| 48 | + |
| 49 | + NLog.LogManager.Configuration = new XmlLoggingConfiguration(path, true); |
| 50 | + NLog.Logger nlog = LogManager.GetCurrentClassLogger(); |
| 51 | + |
| 52 | + for (int i = 0; i < 100; i++) |
| 53 | + { |
| 54 | + // StackifyLib.Logger.Queue("Debug", "Test message"); |
| 55 | + //System.Threading.Thread.Sleep(1); |
| 56 | + nlog.Debug("Hello"); |
| 57 | + //nlog.Debug(new { color = "red", int1 = 1 }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void Log4NetTest() |
| 62 | + { |
| 63 | + XmlDocument log4netConfig = new XmlDocument(); |
| 64 | + |
| 65 | + using (StreamReader reader = new StreamReader(new FileStream("log4net.config", FileMode.Open, FileAccess.Read))) |
| 66 | + { |
| 67 | + log4netConfig.Load(reader); |
| 68 | + } |
| 69 | + |
| 70 | + ILoggerRepository rep = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); |
| 71 | + XmlConfigurator.Configure(rep, log4netConfig["log4net"]); |
| 72 | + |
| 73 | + log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); |
| 74 | + |
| 75 | + for (int i = 0; i < 100; i++) |
| 76 | + { |
| 77 | + // StackifyLib.Logger.Queue("Debug", "Test message"); |
| 78 | + //System.Threading.Thread.Sleep(1); |
| 79 | + log.Debug(new { color = "red", int1 = 1 }); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + private static void StackifyAPILogger_OnLogMessage(string data) |
| 85 | + { |
| 86 | + Debug.WriteLine(data); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments