diff --git a/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchSinkState.cs b/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchSinkState.cs index 4ee6a353..b6108e03 100644 --- a/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchSinkState.cs +++ b/src/Serilog.Sinks.Elasticsearch/Sinks/ElasticSearch/ElasticsearchSinkState.cs @@ -80,6 +80,7 @@ private ElasticsearchSinkState(ElasticsearchSinkOptions options) if (options.ModifyConnectionSettings != null) configuration = options.ModifyConnectionSettings(configuration); + configuration.ThrowExceptions(); _client = new ElasticLowLevelClient(configuration); _formatter = options.CustomFormatter ?? new ElasticsearchJsonFormatter( @@ -120,10 +121,10 @@ public void RegisterTemplateIfNeeded() try { - var templateExistsResponse = this._client.IndicesExistsTemplateForAll(this._templateName); + var templateExistsResponse = this._client.IndicesExistsTemplateForAll(this._templateName); if (templateExistsResponse.HttpStatusCode == 200) return; - var result = this._client.IndicesPutTemplateForAll(this._templateName, new + var result = this._client.IndicesPutTemplateForAll(this._templateName, new { template = this._templateMatchString, settings = new Dictionary diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTestsBase.cs b/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTestsBase.cs index a6ebc8ad..394f63ef 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTestsBase.cs +++ b/test/Serilog.Sinks.Elasticsearch.Tests/ElasticsearchSinkTestsBase.cs @@ -41,7 +41,9 @@ protected ElasticsearchSinkTestsBase() .ReturnsLazily((RequestData requestData) => { MemoryStream ms = new MemoryStream(); - requestData.PostData.Write(ms, new ConnectionConfiguration()); + if(requestData.PostData != null) + requestData.PostData.Write(ms, new ConnectionConfiguration()); + switch (requestData.Method) { case HttpMethod.PUT: @@ -54,7 +56,7 @@ protected ElasticsearchSinkTestsBase() _seenHttpHeads.Add(_templateExistsReturnCode); break; } - return new ElasticsearchResponse(200, new[] { 200 }); + return new ElasticsearchResponse(_templateExistsReturnCode, new[] { 200, 404 }); }); } diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/Serilog.Sinks.Elasticsearch.Tests.csproj b/test/Serilog.Sinks.Elasticsearch.Tests/Serilog.Sinks.Elasticsearch.Tests.csproj index 2087a4fe..d84d518d 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/Serilog.Sinks.Elasticsearch.Tests.csproj +++ b/test/Serilog.Sinks.Elasticsearch.Tests/Serilog.Sinks.Elasticsearch.Tests.csproj @@ -89,14 +89,15 @@ + Designer - + Always - + diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/Templating/SendsTemplateTests.cs b/test/Serilog.Sinks.Elasticsearch.Tests/Templating/SendsTemplateTests.cs index 6b0871ec..572656b2 100644 --- a/test/Serilog.Sinks.Elasticsearch.Tests/Templating/SendsTemplateTests.cs +++ b/test/Serilog.Sinks.Elasticsearch.Tests/Templating/SendsTemplateTests.cs @@ -48,11 +48,8 @@ public void TemplatePutToCorrectUrl() protected void JsonEquals(string json, MethodBase method, string fileName = null) { - var file = this.GetFileFromMethod(method, fileName); - var exists = File.Exists(file); - exists.Should().BeTrue(file + "does not exist"); - - var expected = File.ReadAllText(file); + var expected = TestDataHelper.ReadEmbeddedResource(Assembly.GetExecutingAssembly(), "template.json"); + var nJson = JObject.Parse(json); var nOtherJson = JObject.Parse(expected); var equals = JToken.DeepEquals(nJson, nOtherJson); @@ -60,15 +57,5 @@ protected void JsonEquals(string json, MethodBase method, string fileName = null expected.Should().BeEquivalentTo(json); } - protected string GetFileFromMethod(MethodBase method, string fileName) - { - var type = method.DeclaringType; - var @namespace = method.DeclaringType.Namespace; - var folderSep = Path.DirectorySeparatorChar.ToString(); - var folder = @namespace.Replace("Serilog.Sinks.Elasticsearch.Tests.", "").Replace(".", folderSep); - var file = Path.Combine(folder, (fileName ?? method.Name).Replace(@"\", folderSep) + ".json"); - file = Path.Combine(Environment.CurrentDirectory.Replace("bin" + folderSep + "Debug", "").Replace("bin" + folderSep + "Release", ""), file); - return file; - } } } \ No newline at end of file diff --git a/test/Serilog.Sinks.Elasticsearch.Tests/TestDataHelper.cs b/test/Serilog.Sinks.Elasticsearch.Tests/TestDataHelper.cs new file mode 100644 index 00000000..dd1b37a1 --- /dev/null +++ b/test/Serilog.Sinks.Elasticsearch.Tests/TestDataHelper.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Serilog.Sinks.Elasticsearch.Tests +{ + public static class TestDataHelper + { + public static string ReadEmbeddedResource( + Assembly assembly, + string embeddedResourceNameEndsWith) + { + var resourceNames = assembly.GetManifestResourceNames(); + var resourceName = resourceNames.SingleOrDefault(n => n.EndsWith(embeddedResourceNameEndsWith)); + + if (string.IsNullOrEmpty(resourceName)) + { + throw new ArgumentException( + string.Format( + "Could not find embedded resouce name that ends with '{0}', only found these: {1}", + embeddedResourceNameEndsWith, + string.Join(", ", resourceNames)), + "embeddedResourceNameEndsWith"); + } + + using (var stream = assembly.GetManifestResourceStream(resourceName)) + { + if (stream == null) + { + throw new ArgumentException( + string.Format("Failed to open embedded resource stream for resource '{0}'", resourceName)); + } + + using (var streamReader = new StreamReader(stream)) + { + return streamReader.ReadToEnd(); + } + } + } + } +}