Skip to content

Instantly share code, notes, and snippets.

@ThomasArdal
Last active December 17, 2015 22:10
Show Gist options
  • Save ThomasArdal/5680420 to your computer and use it in GitHub Desktop.
Save ThomasArdal/5680420 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Configuration;
using System.Globalization;
using Nest;
namespace Elmah.ElasticSearch
{
public class ElasticSearchLog : ErrorLog
{
ElasticClient _elasticClient;
public ElasticSearchLog(IDictionary config)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
InitElasticSearch(config);
}
public override string Log(Error error)
{
var indexResponse = _elasticClient.Index(new ErrorDocument
{
ApplicationName = ApplicationName,
Error = error,
ErrorXml = ErrorXml.EncodeString(error)
});
return indexResponse.Id;
}
public override ErrorLogEntry GetError(string id)
{
var document = _elasticClient.Get<ErrorDocument>(id);
var result = !string.IsNullOrEmpty(document.ErrorXml)
? new ErrorLogEntry(this, id, ErrorXml.DecodeString(document.ErrorXml))
: new ErrorLogEntry(this, id, document.Error);
return result;
}
public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
{
var result = _elasticClient.Search<ErrorDocument>(x =>
{
if (!string.IsNullOrWhiteSpace(ApplicationName))
{
x.Filter(f => f.Term(t => t.ApplicationName, ApplicationName));
}
x.Skip(pageSize*pageIndex).Take(pageSize).Sort(s => s.OnField(e => e.Error.Time).Descending());
return x;
});
foreach (var errorDocument in result.Documents)
{
errorEntryList.Add(new ErrorLogEntry(this, errorDocument.Id.ToString(CultureInfo.InvariantCulture), errorDocument.Error));
}
return result.Total;
}
private string LoadConnectionString(IDictionary config)
{
// From ELMAH source
// First look for a connection string name that can be
// subsequently indexed into the <connectionStrings> section of
// the configuration to get the actual connection string.
var connectionStringName = (string)config["connectionStringName"];
if (!string.IsNullOrEmpty(connectionStringName))
{
var settings = ConfigurationManager.ConnectionStrings[connectionStringName];
if (settings != null)
return settings.ConnectionString;
throw new ApplicationException(string.Format("Could not find a ConnectionString with the name '{0}'.", connectionStringName));
}
throw new ApplicationException("You must specifiy the 'connectionStringName' attribute on the <errorLog /> element.");
}
private void InitElasticSearch(IDictionary config)
{
var defaultIndex = "elmah";
var url = LoadConnectionString(config);
var connectionSettings = new ConnectionSettings(new Uri(url));
connectionSettings.SetDefaultIndex(defaultIndex);
_elasticClient = new ElasticClient(connectionSettings);
ConnectionStatus status;
_elasticClient.TryConnect(out status);
if (!status.Success)
{
throw new ApplicationException(
string.Format("Could not connect to ElasticSearch: " +
(status.Error != null ? status.Error.ExceptionMessage : "Unknown reason")));
}
}
}
[ElasticType]
public class ErrorDocument
{
public int Id { get; set; }
public Error Error { get; set; }
public string ErrorXml { get; set; }
public string ApplicationName { get; set; }
}
}
@ThomasArdal
Copy link
Author

After some debugging I found out, that it's a property of type Exception, that causes the problem in MapFluent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment