Log to elmah.io from NLog.
Documentation: Logging to elmah.io from NLog
NLog target for elmah.io
Home Page: https://elmah.io
License: Apache License 2.0
Log to elmah.io from NLog.
Documentation: Logging to elmah.io from NLog
Instead of parsing properties multiple times. Then it could be done once (without allocating extra strings):
private List<Item> PropertiesToData(LogEventInfo logEvent,
ref string application,
ref string source,
ref string hostname,
ref string user,
ref string method,
ref string version,
ref string url,
ref string type,
ref string statuscode)
{
if (!logEvent.HasProperties) return null;
var items = new List<Item>(logEvent.Properties.Count);
foreach (var obj in logEvent.Properties)
{
if (obj.Value != null)
{
var propertyName = obj.Key.ToString();
var propertyValue = obj.Value.ToString();
if (string.Equals("application", propertyName, StringComparison.OrdinalIgnoreCase))
application = propertyValue;
else if (string.Equals("source", propertyName, StringComparison.OrdinalIgnoreCase))
source = propertyValue;
else if (string.Equals("hostname", propertyName, StringComparison.OrdinalIgnoreCase))
hostname = propertyValue;
else if (string.Equals("user", propertyName, StringComparison.OrdinalIgnoreCase))
user = propertyValue;
else if (string.Equals("method", propertyName, StringComparison.OrdinalIgnoreCase))
method = propertyValue;
else if (string.Equals("version", propertyName, StringComparison.OrdinalIgnoreCase))
version = propertyValue;
else if (string.Equals("url", propertyName, StringComparison.OrdinalIgnoreCase))
version = propertyValue;
else if (string.Equals("type", propertyName, StringComparison.OrdinalIgnoreCase))
type = propertyValue;
else if (string.Equals("statuscode", propertyName, StringComparison.OrdinalIgnoreCase))
statuscode = propertyValue;
items.Add(new Item { Key = propertyName, Value = propertyValue });
}
}
return items;
}
It could be really neat if CreateMessage-object accepted ICollection<Item>
instead of IList<Item>
. Then you could just convert LogEventInfo.Properties
into Dictionary<string,Item>
with StringComparer.OrdinalIgnoreCase
as comparer. Then you could return Dictionary.Values
as items-collection.
You should also fix LevelToSeverity
so it doesn't allocate every time:
private string LevelToSeverity(LogLevel level)
{
if (level == LogLevel.Debug) return nameof(Severity.Debug);
if (level == LogLevel.Error) return nameof(Severity.Error);
if (level == LogLevel.Fatal) return nameof(Severity.Fatal);
if (level == LogLevel.Trace) return nameof(Severity.Verbose);
if (level == LogLevel.Warn) return nameof(Severity.Warning);
return nameof(Severity.Information);
}
You should add ${exception:format=Method}
:
Btw. what is "type" used for (Is it like ${exception:format=type}
) ?
NLog 4.5 is going to be the next official release.
netStandard1.6
to netStandard1.5
Change this line:
Data = PropertiesToData(logEvent.Properties),
Into this line:
Data = PropertiesToData(logEvent),
And modify PropertiesToData like this:
public static readonly Item[] EmptyArray = new Item[0];
private IList<Item> PropertiesToData(LogEventInfo logEvent)
{
if (!logEvent.HasProperties)
return EmptyArray; // Replace with Array.Empty with net46
var properties = logEvent.Properties;
var items = new List<Item>(properties.Count);
foreach (var obj in properties)
{
if (obj.Value != null)
{
items.Add(new Item { Key = obj.Key.ToString(), Value = obj.Value.ToString() });
}
}
return items;
}
This will allow UWP10 application to work aswell:
You no longer have to specify all letter-case combinations:
The same for MDLC / MDC / GDC.
Btw. MDLC / MDC have become obsolete with NLog 5, and both are replaced by ${scopeproperty=propname}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.