/// <summary> /// Adds network logging. /// </summary> public static void UseLog2ConsoleLogging() { ChainsawTarget target = new ChainsawTarget(); target.AppInfo = "Roadkill"; target.Address = "udp://127.0.0.1:7071"; AddNLogTarget(target, "RoadkillLog2Console"); }
private static void AddChainsawLogging(LoggingConfiguration configuration) { const string TargetName = "Chainsaw"; var target = new ChainsawTarget() { Name = TargetName, Address = "udp://127.0.0.1:7071" }; var rule = new LoggingRule("*", LogLevel.Trace, target); configuration.AddTarget(TargetName, target); configuration.LoggingRules.Add(rule); }
static void Main(string[] args) { ChainsawTarget target = new ChainsawTarget(); target.Address = "udp://localhost:4000"; NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); Logger logger = LogManager.GetLogger("Example"); logger.Trace("log message 1"); logger.Debug("log message 2"); logger.Info("log message 3"); logger.Warn("log message 4"); logger.Error("log message 5"); logger.Fatal("log message 6"); }
public Log() { var config = new LoggingConfiguration(); var consoleTarget = new ColoredConsoleTarget { Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}" }; var chainsawTarget = new ChainsawTarget { Address = "udp://127.0.0.1:7071", Name = "chainsaw", Layout = "Log4JXmlEventLayout" }; config.AddTarget("console", consoleTarget); config.AddTarget("chainsaw", chainsawTarget); config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget)); config.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, chainsawTarget)); LogManager.Configuration = config; Logger = LogManager.GetCurrentClassLogger(); }
internal static void ConfigureLogging( LogDelegates.LogMethod logMethod , bool proxyLogging = false ) { // logMethod ( // Resource // .AppLoggingConfigHelper_ConfigureLogging_____Starting_logger_configuration_ // ) ; LogFactory proxiedFactory = null; if (proxyLogging) { var proxyGenerator = new ProxyGenerator(); var loggerProxyHelper = new LoggerProxyHelper(proxyGenerator, DoLogMessage); var logFactory = new MyLogFactory(DoLogMessage); var lConfLogFactory = loggerProxyHelper.CreateLogFactory(logFactory); proxiedFactory = lConfLogFactory; } var fieldInfo = typeof(LogManager).GetField( "factory" , BindingFlags.Static | BindingFlags.NonPublic ); if (fieldInfo != null) { logMethod($"field info is {fieldInfo.DeclaringType} . {fieldInfo.Name}"); var cur = fieldInfo.GetValue(null); logMethod($"cur is {cur}"); if (proxyLogging) { fieldInfo.SetValue(null, proxiedFactory); var newVal = fieldInfo.GetValue(null); logMethod($"New Value = {newVal}"); } } // var useFactory = proxyLogging ? proxiedFactory : LogManager.LogFactory; var lConf = new CodeConfiguration(); var dict = LogLevel.AllLoggingLevels.ToDictionary( level => level , level => new List <Target>() ); var errorTargets = dict[LogLevel.Error]; var t = dict[LogLevel.Trace]; // var x = new EventLogTarget("eventLog") { Source = "Application Error" }; // errorTargets.Add(x); var webServiceTarget = new LogReceiverWebServiceTarget("log"); webServiceTarget.EndpointAddress = "http://xx1.mynetgear.com/LogService/ReceiveLogs.svc";// "http://localhost:27809/ReceiveLogs.svc"; // webServiceTarget.EndpointConfigurationName = "log"; dict[LogLevel.Debug].Add(webServiceTarget); #region Cache Target #if false var cacheTarget = new MyCacheTarget( ); t.Add(cacheTarget); #endif #endregion #region NLogViewer Target var viewer = Viewer(); t.Add(viewer); #endregion #region Debugger Target if (DebuggerTargetEnabled) { var debuggerTarget = new DebuggerTarget { Layout = new SimpleLayout("${message}") }; t.Add(debuggerTarget); } #endregion #region Chainsaw Target var chainsawTarget = new ChainsawTarget( ); SetupNetworkTarget(chainsawTarget, "udp://xx1.mynetgear.com:4445"); t.Add(chainsawTarget); #endregion t.Add(MyFileTarget()); var jsonFileTarget = JsonFileTarget(); t.Add(jsonFileTarget); var byType = new Dictionary <Type, int>(); foreach (var target in dict.SelectMany(pair => pair.Value)) { var type = target.GetType(); byType.TryGetValue(type, out var count); count += 1; byType[type] = count; if (target.Name == null) { target.Name = $"{Regex.Replace(type.Name, "Target", "")}{count:D2}"; } lConf.AddTarget(target); } foreach (var result in dict.Select(LoggingRule)) { ((List <LoggingRule>)lConf.LoggingRules).AddRange(result); } LogManager.Configuration = lConf; Logger = LogManager.GetCurrentClassLogger(); }