Elasticsearch.NET

Logging made easy

July 2015

Presented by Steven Salat

# Wat? * Database focused on immediate, actionable insight from data * Full-text search (built on Apache Lucene) * Document-oriented (complex JSON objects) * Schema-free (think NoSQL) * REST-ful API (JSON over HTTP) * Open source Apache license, code is on [GitHub](https://github.com/elastic/elasticsearch-net)
# When? Elasticsearch is great for: * Exception Logs * HTTP Access Logs * Web Service incoming calls * Web Service outgoing calls * Maybe timing/performance analytics!
# Where * Elasticsearch works everywhere HTTP does * Elasticsearch works great with: * **Logstash** - read log files and write to Elasticsearch * **Kibana** - visualize data with charts, queries, etc
# How? - Install Simply install the nuget package ```sh PM> Install-Package Elasticsearch.Net ```
# How? - Usage Example of error message logged to Elasticsearch database. ```cs var node = new Uri("http://example-elastic:9987"); var config = new ConnectionConfiguration(node); var client = new ElasticsearchClient(config); var o = new { @Date = DateTime.UtcNow, // Use UTC to avoid time zones @Uri = uri, // Full URI where error occurred @Exception = ex, // Exception object (message, stacktrace) @MachineName = Environment.MachineName, // server name @ServerVars = serverVars // Dictionary of other vars }); var json = Newtonsoft.Json.JsonConvert.SerializeObject(o); client.Index("AppName", "ErrorType", json); ```
# Lets "Wrap" It Up ```cs /// <summary> /// Log data to ElasticSearch. Read about types here: https://www.elastic.co/guide/en/elasticsearch/guide/master/mapping.html /// </summary> /// <param name="type">A type in Elasticsearch represents a class of similar documents.</param> /// <param name="o">The Object to log of the specified type</param> private static void Log(string type, object o) { if (ConfigurationManager.AppSettings["ElasticSearchEnabled"] == "1") { var node = new Uri(ConfigurationManager.AppSettings["ElasticSearchUrl"]); var config = new ConnectionConfiguration(node); var client = new ElasticsearchClient(config); var json = Newtonsoft.Json.JsonConvert.SerializeObject(o); var d = client.Index("AppName", type, json); } } ``` * We hard code the `Index` as "AppName" but allow different types for different logs.
# Demo * Show usage in WDITR * Demo Kibana queries