public void ExceptionSerializationTest(bool hasExceptionFieldLayout) { using (var testOutputTextWriter = new TestOutputTextWriter(testOutputHelper)) { InternalLogger.LogWriter = testOutputTextWriter; InternalLogger.LogLevel = LogLevel.Warn; var elasticTarget = new ElasticSearchTarget(); if (hasExceptionFieldLayout) { elasticTarget.Fields.Add(new Field { Name = "exception", Layout = Layout.FromString("${exception:format=toString,Data:maxInnerExceptionLevel=10}"), LayoutType = typeof(string) }); } var rule = new LoggingRule("*", LogLevel.Info, elasticTarget); var config = new LoggingConfiguration(); config.LoggingRules.Add(rule); LogManager.ThrowExceptions = true; LogManager.Configuration = config; var logger = LogManager.GetLogger("Example"); logger.Error(new BadLogException(), "Boom"); LogManager.Flush(); } }
/// <summary> /// 获取日志列表 /// </summary> /// <param name="logContent">日志内容</param> /// <param name="logType">日志类型</param> /// <param name="level">日志级别</param> /// <param name="opUserName">操作人用户名</param> /// <param name="startTime">开始时间</param> /// <param name="endTime">结束时间</param> /// <param name="pagination">分页参数</param> /// <returns></returns> public List <Base_Log> GetLogList( Pagination pagination, string logContent, string logType, string level, string opUserName, DateTime?startTime, DateTime?endTime) { ILogSearcher logSearcher = null; if (GlobalSwitch.LoggerType.HasFlag(LoggerType.RDBMS)) { logSearcher = new RDBMSTarget(); } else if (GlobalSwitch.LoggerType.HasFlag(LoggerType.ElasticSearch)) { logSearcher = new ElasticSearchTarget(); } else { throw new Exception("请指定日志类型为RDBMS或ElasticSearch!"); } return(logSearcher.GetLogList(pagination, logContent, logType, level, opUserName, startTime, endTime)); }
public static LoggingConfiguration UseElastic(this LoggingConfiguration configuration, LogConfig config) { if (!string.IsNullOrEmpty(config.Elastic?.Uri)) { if (config.HostName.Contains("-")) { throw new InvalidOperationException($"Dash character (-) is not allowed in the Logging.HostName property. Please check your application settings file."); } var target = new ElasticSearchTarget { Name = "Elastic", CloudId = config.Elastic.CloudId, Username = config.Elastic.Username, Password = config.Elastic.Password, RequireAuth = true, Uri = config.Elastic.Uri, Index = $"logs-{config.HostName}", Fields = new List <Field> { new Field { Name = "host.name", Layout = config.HostName }, new Field { Name = "application", Layout = config.ApplicationName } }, Layout = "${message}" }; configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.FromString(config.Elastic.LogLevel ?? "Trace"), target)); } return(configuration); }
public void ExceptionTest() { var elasticTarget = new ElasticSearchTarget(); elasticTarget.User = "******"; elasticTarget.Password = "******"; var rule = new LoggingRule("*", elasticTarget); rule.EnableLoggingForLevel(LogLevel.Error); var config = new LoggingConfiguration(); config.LoggingRules.Add(rule); LogManager.Configuration = config; var logger = LogManager.GetLogger("Example"); var exception = new ArgumentException("Some random error message"); logger.Error(exception, "An exception occured"); LogManager.Flush(); }
public LoggingBase(ElasticSearchTarget elastictarget, NLog.LogLevel logLevelMin, NLog.LogLevel logLevelMax) { // Rules for mapping loggers to targets //config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logelastic); //// Apply config //NLog.LogManager.Configuration = config; //logger = NLog.LogManager.GetCurrentClassLogger(); config = new NLog.Config.LoggingConfiguration(); logelastic = elastictarget; //Standardfelder hinzufügen logelastic.Fields.Add(new Field() { Name = "Logger", Layout = "${logger}" }); logelastic.Fields.Add(new Field() { Name = "Host", Layout = "${hostname}" }); logelastic.Fields.Add(new Field() { Name = "CallSite", Layout = "${callsite:className=true:filename=false:includeNamespace=false}" }); logelastic.Fields.Add(new Field() { Name = "ThreadId", Layout = "${threadid}" }); logelastic.Fields.Add(new Field() { Name = "Exception", Layout = "${exception}" }); logelastic.Fields.Add(new Field() { Name = "StackTrace", Layout = "${stacktrace}" }); // Rules for mapping loggers to targets config.AddRule(logLevelMin, logLevelMax, logelastic); // Apply config NLog.LogManager.Configuration = config; loggerBase = NLog.LogManager.GetCurrentClassLogger(); loggerBase.WithProperty("Prozess", loggerBase.Name).Info($"LoggingBase Klasse initialisiert"); }
public void DeleteLog(string logContent, string logType, string level, string opUserName, DateTime?startTime, DateTime?endTime) { ILogDeleter logDeleter; if (GlobalSwitch.LoggerType.HasFlag(LoggerType.RDBMS)) { logDeleter = new RDBMSTarget(); logDeleter.DeleteLog(logContent, logType, level, opUserName, startTime, endTime); } if (GlobalSwitch.LoggerType.HasFlag(LoggerType.ElasticSearch)) { logDeleter = new ElasticSearchTarget(); logDeleter.DeleteLog(logContent, logType, level, opUserName, startTime, endTime); } }
public MonitoringTest() { ElasticSearchTarget logelasticlogelastic = new ElasticSearchTarget { Name = "elastic", Uri = "http://jhistorian.prod.j1:9200/", //Uri = "http://192.168.2.41:32120", Index = "JusiBase-${level}-${date:format=yyyy-MM-dd}", //Index = "historianWriter-${level}-${date:format=yyyy-MM-dd}", //Layout = "${logger} | ${threadid} | ${message}", Layout = "${message}", IncludeAllProperties = true, }; JusiBase.LoggingBase logging = new LoggingBase(logelasticlogelastic, NLog.LogLevel.Debug, NLog.LogLevel.Fatal); }
public void SimpleLogTest() { var elasticTarget = new ElasticSearchTarget(); var rule = new LoggingRule("*", elasticTarget); rule.EnableLoggingForLevel(LogLevel.Info); var config = new LoggingConfiguration(); config.LoggingRules.Add(rule); LogManager.Configuration = config; var logger = LogManager.GetLogger("Example"); logger.Info("Hello elasticsearch"); LogManager.Flush(); }
private static LoggingConfiguration LoggingConfiguration() { var configuration = new LoggingConfiguration(); var console = new ColoredConsoleTarget(); configuration.AddTarget("console", console); configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, console)); var file = new FileTarget(); file.FileName = "${basedir}/app.log"; configuration.AddTarget("file", file); configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, file)); var elastic = new ElasticSearchTarget(); configuration.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, elastic)); console.Layout = file.Layout = elastic.Layout = @"${date} [${threadid}] ${logger} ${message}"; return(configuration); }
public void SimpleJsonLayoutTest() { var elasticTarget = new ElasticSearchTarget(); elasticTarget.EnableJsonLayout = true; elasticTarget.Layout = new JsonLayout() { MaxRecursionLimit = 10, IncludeAllProperties = true, Attributes = { new JsonAttribute("timestamp", "${date:universaltime=true:format=o}"), new JsonAttribute("lvl", "${level}"), new JsonAttribute("msg", "${message}"), new JsonAttribute("logger", "${logger}"), new JsonAttribute("threadid", "${threadid}", false), // Skip quotes for integer-value } }; var rule = new LoggingRule("*", elasticTarget); rule.EnableLoggingForLevel(LogLevel.Info); var config = new LoggingConfiguration(); config.LoggingRules.Add(rule); LogManager.ThrowExceptions = true; LogManager.Configuration = config; var logger = LogManager.GetLogger("Example"); logger.Info("Hello elasticsearch"); LogManager.Flush(); }
public void CustomJsonConverterExceptionTest() { var runner = MongoDbRunner.Start(); try { var dbClient = new MongoClient(runner.ConnectionString); var database = dbClient.GetDatabase("Test"); var collection = database.GetCollection <TestModel>("TestCollection"); collection .Indexes .CreateOneAsync( Builders <TestModel> .IndexKeys.Ascending(a => a.NoDuplicate), new CreateIndexOptions { Unique = true }); ElasticSearchTarget.AddJsonConverter(new JsonToStringConverter(typeof(IPAddress))); using (var testOutputTextWriter = new TestOutputTextWriter(testOutputHelper)) { InternalLogger.LogWriter = testOutputTextWriter; InternalLogger.LogLevel = LogLevel.Error; LogManager.Configuration = new XmlLoggingConfiguration("NLog.Targets.ElasticSearch.Tests.dll.config"); var logger = LogManager.GetLogger("Example"); var testModel1 = new TestModel { _id = ObjectId.GenerateNewId(), NoDuplicate = "AAA" }; collection.InsertOne(testModel1); var exception = Assert.Throws <MongoCommandException>(() => { var testModel2 = new TestModel { _id = ObjectId.GenerateNewId(), NoDuplicate = "AAA" }; collection.FindOneAndReplace( Builders <TestModel> .Filter.Eq(a => a._id, ObjectId.GenerateNewId()), testModel2, new FindOneAndReplaceOptions <TestModel, TestModel> { ReturnDocument = ReturnDocument.Before, IsUpsert = true }); }); logger.Error(exception, "Failed to insert data"); LogManager.Flush(); Assert.False(testOutputTextWriter.HadErrors(), "Failed to log to elastic"); } } finally { runner.Dispose(); } }