Skip to content

Commit 0ebb933

Browse files
authored
Merge pull request #64 from amccool/logerror-message
dont overwrite the logger message with the exception.message
2 parents e7ad0c0 + 04407a0 commit 0ebb933

File tree

5 files changed

+27
-95
lines changed

5 files changed

+27
-95
lines changed

example/SampleApp/App.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public async Task Run()
5252
_logger.LogWarning("Run...................");
5353
}
5454

55-
_logger.LogInformation($"Ending Service for with LogKey {logKey}");
55+
_logger.LogInformation("Ending Service for with LogKey {logKey}", logKey);
5656
}
5757

58-
private async Task Breakstuff()
58+
private Task Breakstuff()
5959
{
6060
int x = 0;
6161
try
@@ -64,8 +64,9 @@ private async Task Breakstuff()
6464
}
6565
catch (Exception e)
6666
{
67-
_logger.LogError(100, e, "sdgsgsgsg", 1, "3");
67+
_logger.LogError(100, e, "sdgsgsgsg {a} {b}", 1, "sdfsdgfsg");
6868
}
69+
return Task.CompletedTask;
6970
}
7071
}
7172
}

example/SampleApp/Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public Program()
4040
.AddElasticSearch(options =>
4141
{
4242
//options.ElasticsearchEndpoint = new Uri(@"http://localhost:9200/");
43-
//options.ElasticsearchEndpoint = new Uri(@"https://elasticsearch.mgmc.rauland/");
4443
options.ElasticsearchEndpoint = new Uri(@"http://es.devint.dev-r5ead.net:9200/");
45-
//options.IndexName = "trace";
4644
});
4745
});
4846

@@ -78,6 +76,17 @@ public void Runner(string[] args)
7876
logger.LogTrace("Starting application");
7977
logger.LogWarning("Starting application");
8078

79+
try
80+
{
81+
int x = 0;
82+
var y = 100 / x;
83+
}
84+
catch (Exception ex)
85+
{
86+
logger.LogCritical(new EventId(10, "cccc"), ex, "blah blah {junk}", 19999999);
87+
//throw;
88+
}
89+
8190

8291
//do the actual work here
8392
var bar = _serviceProvider.GetService<IApp>();

src/ElasticLogger.Test/Startup.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
2727

2828
await next.Invoke();
2929
});
30-
31-
//app.UseMvcWithDefaultRoute();
32-
//app.useco .AddControllers
3330
}
3431
}
3532
}

src/ElasticSearch.Extensions.Logging/ElasticSearchLogger.cs

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
namespace AM.Extensions.Logging.ElasticSearch
1414
{
15-
public class ElasticsearchLogger : ILogger
15+
internal sealed class ElasticsearchLogger : ILogger
1616
{
17-
1817
private readonly LogLevel _logLevel;
1918
private readonly string _userDomainName;
2019
private readonly string _userName;
@@ -32,27 +31,9 @@ public ElasticsearchLogger(string categoryName, Action<JObject> scribeProcessor)
3231
_userDomainName = Environment.UserDomainName;
3332
_userName = Environment.UserName;
3433
_machineName = Environment.MachineName;
35-
3634
}
3735

38-
//public ElasticsearchLogger(string categoryName, Uri endpoint, string indexPrefix)
39-
//{
40-
// Name = categoryName;
41-
42-
// _endpoint = endpoint;
43-
// _indexPrefix = indexPrefix;
44-
45-
// // Default is to turn on all the logging
46-
// _logLevel = LogLevel.Trace;
47-
48-
// _userDomainName = Environment.UserDomainName;
49-
// _userName = Environment.UserName;
50-
// _machineName = Environment.MachineName;
51-
// Initialize();
52-
//}
53-
5436
public string Name { get; }
55-
5637

5738

5839
public IDisposable BeginScope<TState>(TState state)
@@ -90,80 +71,22 @@ public void Log<TState>(LogLevel logLevel, EventId eventId,
9071
WriteTrace(Name, logLevel, eventId.Id, message, Guid.Empty, exception);
9172
}
9273

93-
protected void WriteTrace(
74+
private void WriteTrace(
9475
string loggerName,
9576
LogLevel eventType,
9677
int id,
9778
string message,
9879
Guid? relatedActivityId,
99-
object data)
80+
Exception data)
10081
{
101-
string updatedMessage = message;
10282
JObject payload = null;
10383
var serializerIgnoreReferenceLoop = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
10484
if (data != null)
10585
{
106-
if (data is Exception)
107-
{
108-
updatedMessage = ((Exception)data).Message;
109-
110-
payload = JObject.FromObject(data, serializerIgnoreReferenceLoop);
111-
}
112-
else if (data is XPathNavigator)
113-
{
114-
var xdata = data as XPathNavigator;
115-
//xdata.MoveToRoot();
116-
117-
XDocument xmlDoc;
118-
try
119-
{
120-
xmlDoc = XDocument.Parse(xdata.OuterXml);
121-
122-
}
123-
catch (Exception)
124-
{
125-
xmlDoc = XDocument.Parse(xdata.ToString());
126-
//eat
127-
//throw;
128-
}
129-
130-
// Convert the XML document in to a dynamic C# object.
131-
dynamic xmlContent = new ExpandoObject();
132-
ExpandoObjectHelper.Parse(xmlContent, xmlDoc.Root);
133-
134-
string json = JsonConvert.SerializeObject(xmlContent, new JsonSerializerSettings{ReferenceLoopHandling = ReferenceLoopHandling.Ignore});
135-
payload = JObject.Parse(json);
136-
}
137-
else if (data is DateTime)
138-
{
139-
payload = new JObject();
140-
payload.Add("System.DateTime", (DateTime)data);
141-
}
142-
else if (data is string)
143-
{
144-
payload = new JObject();
145-
payload.Add("string", (string)data);
146-
}
147-
else if (data.GetType().IsValueType)
148-
{
149-
payload = new JObject { { "data", data.ToString() } };
150-
}
151-
else
152-
{
153-
try
154-
{
155-
payload = JObject.FromObject(data, serializerIgnoreReferenceLoop);
156-
}
157-
catch (JsonSerializationException jEx)
158-
{
159-
payload = new JObject();
160-
payload.Add("FAILURE", jEx.Message);
161-
payload.Add("data", data.GetType().ToString());
162-
}
163-
}
86+
payload = JObject.FromObject(data, serializerIgnoreReferenceLoop);
16487
}
16588

166-
InternalWrite(new TraceEventCache(), loggerName, eventType, id, updatedMessage, relatedActivityId, payload);
89+
InternalWrite(new TraceEventCache(), loggerName, eventType, id, message, relatedActivityId, payload);
16790
}
16891

16992
private void InternalWrite(
@@ -172,8 +95,7 @@ private void InternalWrite(
17295
LogLevel eventType,
17396
int? traceId,
17497
string message,
175-
Guid?
176-
relatedActivityId,
98+
Guid? relatedActivityId,
17799
JObject dataObject)
178100
{
179101
DateTime logTime;

src/ElasticSearch.Extensions.Logging/ElasticSearchLoggerProvider.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ private async Task WriteDirectlyToES(JObject jo)
131131
//POST /_bulk? filter_path = items.*.error
132132
private static Dictionary<string, object> filter_path = new Dictionary<string, object>() { { "filter_path", "items.*.error" } };
133133

134-
private async Task WriteDirectlyToESAsBatch(IEnumerable<JObject> jos)
134+
private Task WriteDirectlyToESAsBatch(IEnumerable<JObject> jos)
135135
{
136136
if (!jos.Any())
137-
return;
137+
return Task.CompletedTask;
138138

139139
var indx = new { index = new { _index = Index, _type = DocumentType } };
140140
var indxC = Enumerable.Repeat(indx, jos.Count());
@@ -156,11 +156,12 @@ private async Task WriteDirectlyToESAsBatch(IEnumerable<JObject> jos)
156156
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
157157
TaskScheduler.Current);
158158

159+
return Task.CompletedTask;
159160
}
160161

161162
private void WriteToQueueForProcessing(JObject jo)
162163
{
163-
this._queueToBePosted.Add(jo);
164+
_queueToBePosted.Add(jo);
164165
}
165166

166167
#region IDisposable Support
@@ -173,6 +174,8 @@ protected virtual void Dispose(bool disposing)
173174
if (disposing)
174175
{
175176
// TODO: dispose managed state (managed objects).
177+
//_client.
178+
_queueToBePosted.Dispose();
176179
}
177180

178181
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.

0 commit comments

Comments
 (0)