Git Product home page Git Product logo

elmah.io.nlog's Introduction

elmah.io.nlog's People

Contributors

304notmodified avatar damienbod avatar snakefoot avatar thomasardal avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

elmah.io.nlog's Issues

Optimize parsing of NLog Properties and LogLevel

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);
        }

ElmahIoTarget with less allocation

  • No need to allocate MachineNameLayoutRenderer for every logevent
  • No need to allocate IdentityLayoutRenderer for every logevent
  • No need to parse Guid for every logevent (Implement string-property to have Guid as backing-field)

Change to NLog 4.5-rc02 and NetStandard1.5

NLog 4.5 is going to be the next official release.

  • Change dependency from NLog 5.0-beta11 to NLog 4.5-rc02.
  • Change targetframeworks from netStandard1.6 to netStandard1.5

Skip allocation of Item-List for every LogEvent

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;
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.