public void TestIndexTimeout() { var timeout = 1; var s = new ConnectionSettings(Test.Default.Host, Test.Default.Port, timeout) .SetDefaultIndex(Test.Default.DefaultIndex) .SetMaximumAsyncConnections(Test.Default.MaximumAsyncConnections) .UsePrettyResponses(); var client = new ElasticClient(s); var newProject = new ElasticSearchProject { Name = "COBOLES", //COBOL ES client ? }; var t = client.IndexAsync<ElasticSearchProject>(newProject); t.Wait(1000); var cs = t.Result; Assert.False(cs.Success); Assert.NotNull(cs.Error); Assert.NotNull(cs.Error.OriginalException); Trace.WriteLine(cs.Error.OriginalException); Assert.IsNotNullOrEmpty(cs.Error.ExceptionMessage); Assert.IsTrue(cs.Error.OriginalException is WebException); var we = cs.Error.OriginalException as WebException; Assert.IsTrue(cs.Error.ExceptionMessage.Contains("The request was canceled")); Assert.IsTrue(we.Status == WebExceptionStatus.RequestCanceled); Assert.True(t.IsCompleted, "task did not complete"); Assert.False(t.IsFaulted, "task was faulted, wich means the exception did not cleanly pass to ConnectionStatus"); }
/// <summary> /// Add a log event to the ElasticSearch Repo /// </summary> /// <param name="loggingEvent"></param> protected override void Append(Core.LoggingEvent loggingEvent) { if (string.IsNullOrEmpty(ConnectionString)) { var exception = new InvalidOperationException("Connection string not present."); ErrorHandler.Error("Connection string not included in appender.", exception, ErrorCode.GenericFailure); return; } client = new ElasticClient(ConnectionBuilder.BuildElsticSearchConnection(ConnectionString)); LogEvent logEvent = new LogEvent(); if (logEvent == null) { throw new ArgumentNullException("logEvent"); } logEvent.LoggerName = loggingEvent.LoggerName; logEvent.Domain = loggingEvent.Domain; logEvent.Identity = loggingEvent.Identity; logEvent.ThreadName = loggingEvent.ThreadName; logEvent.UserName = loggingEvent.UserName; logEvent.MessageObject = loggingEvent.MessageObject == null ? "" : loggingEvent.MessageObject.ToString(); logEvent.TimeStamp = loggingEvent.TimeStamp; logEvent.Exception = loggingEvent.ExceptionObject == null ? "" : loggingEvent.MessageObject.ToString(); logEvent.Message = loggingEvent.RenderedMessage; logEvent.Fix = loggingEvent.Fix.ToString(); logEvent.HostName = Environment.MachineName; if (loggingEvent.Level != null) { logEvent.Level = loggingEvent.Level.DisplayName; } if (loggingEvent.LocationInformation != null) { logEvent.ClassName = loggingEvent.LocationInformation.ClassName; logEvent.FileName = loggingEvent.LocationInformation.FileName; logEvent.LineNumber = loggingEvent.LocationInformation.LineNumber; logEvent.FullInfo = loggingEvent.LocationInformation.FullInfo; logEvent.MethodName = loggingEvent.LocationInformation.MethodName; } logEvent.Properties = loggingEvent.Properties.GetKeys().ToDictionary(key => key, key => logEvent.Properties[key].ToString()); if (client.IsValid) { client.IndexAsync(logEvent); } else { var exception = new InvalidOperationException("Connection to ElasticSearch is invalid."); ErrorHandler.Error("Invalid connection to ElasticSearch", exception, ErrorCode.GenericFailure); return; } }
/// <summary> /// Add a log event to the ElasticSearch Repo /// </summary> /// <param name="loggingEvent"></param> protected override void Append(Core.LoggingEvent loggingEvent) { if (string.IsNullOrEmpty(ConnectionString)) { var exception = new InvalidOperationException("Connection string not present."); ErrorHandler.Error("Connection string not included in appender.", exception, ErrorCode.GenericFailure); return; } var settings = ConnectionBuilder.BuildElsticSearchConnection(ConnectionString); client = new ElasticClient(settings); var logEvent = CreateLogEvent(loggingEvent); try { client.IndexAsync(logEvent, settings.DefaultIndex, "LogEvent"); } catch (InvalidOperationException ex) { ErrorHandler.Error("Invalid connection to ElasticSearch", ex, ErrorCode.GenericFailure); } }
/// <summary> /// Depends on hn_full_11-07-2010.xml which you can download from: /// http://api.ihackernews.com/torrents/hn_full_11-07-2010.zip.torrent /// /// When run from debug make sure to change the default debug arguments. /// <param name="args">Full filepath to hn_full_11-07-2010.xml</param> static void Main(string[] args) { var filePath = args.First(); var elasticSettings = new ConnectionSettings("127.0.0.1.", 9200) .SetDefaultIndex("mpdreamz") .SetMaximumAsyncConnections(50); var client = new ElasticClient(elasticSettings); ConnectionStatus connectionStatus; if (!client.TryConnect(out connectionStatus)) { Console.Error.WriteLine("Could not connect to {0}:\r\n{1}", elasticSettings.Host, connectionStatus.Error.OriginalException.Message); Console.Read(); return; } var reader = new XmlTextReader(filePath); Post post = new Post(); PostMetaData meta = new PostMetaData(); int processed = 0, dropped = 0; Stopwatch sw = new Stopwatch(); sw.Start(); var postQueue = new List<Post>(); try { while (reader.Read()) { var name = reader.Name; if (reader.NodeType == XmlNodeType.Element) { if (name == "HackerNews") continue; if (name == "ID") post.Id = reader.ReadElementContentAsInt(); else if (name == "ParentID") post.ParentId = reader.ReadElementContentAsInt(); else if (name == "Url") post.Url = reader.ReadElementContentAsString(); else if (name == "Title") post.Title = reader.ReadElementContentAsString(); else if (name == "Text") post.Text = reader.ReadElementContentAsString(); else if (name == "Username") meta.Username = reader.ReadElementContentAsString(); else if (name == "Points") meta.Points = reader.ReadElementContentAsInt(); else if (name == "Type") meta.Type = reader.ReadElementContentAsInt(); else if (name == "Timestamp") meta.Created = reader.ReadElementContentAsDateTime(); else if (name == "CommentCount") meta.CommentsCount = reader.ReadElementContentAsInt(); } if (reader.NodeType == XmlNodeType.EndElement && name == "row") { post.Meta = meta; postQueue.Add(post); if (postQueue.Count() == 1000) { client.IndexAsync<Post>(postQueue, (c) => { if (!c.Success) dropped++; }); postQueue = new List<Post>(); processed++; } Console.Write("\rProcessed:{0}, Dropped:{2} in {1}", processed, sw.Elapsed, dropped); post = new Post(); meta = new PostMetaData(); } } sw.Stop(); Console.WriteLine("\nDone! {0}", sw.Elapsed); } catch (Exception e) { } }