GithubHelp home page GithubHelp logo

dev-11 / aop_logging Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 48 KB

An AOP way of logging in dotnet core

Home Page: https://dev-11.github.io/aop_logging/

License: MIT License

C# 100.00%
aop aspect-oriented-programming dotnet-core dotnetcore logging logging-library dispatchproxy

aop_logging's Introduction

aop_logging

Build Status GitHub

A Simple aspect oriented logger

The type T, what we want to decorate with logging, has to implement an interface, cannot be sealed, and cannot be abstract as these are the precondition of the System.Reflection.DispatchProxyGenerator.GenerateProxyType(Type baseType, Type interfaceType) method.

Main Interfaces:

ILogger

The ILogger interface has just a single function: void Log(LogEntry logEntry). How the logging exactly happens is up to the user.

ILogEntryBuilder

Right now the code logs 3 type of events: Invoke, Leave, and Exception. Every event generates a LogEntry which will be logged by the ILogger. There is a default implementation of the ILogEntryBuilder which can be easily replaced by a custom implementation of the ILogEntryBuilder interface.

Example Usage

var calc = LoggingProxy<ICalculator>.Create(new Calculator(),
                                            new ConsoleLogger(),
                                            new LogEntryBuilder());

calc.Add(1, 2);
calc.Divide(12, 0);

If we decorate T with the LoggingProxy the logging will be completely automatic.

Sample output of ConsoleLogger

07/19/2019 19:32:35|Information|Invoke|ClassName: AopLoggingConsole.Calculator, Method: Add, Args: 1, 2
07/19/2019 19:32:35|Information|Leave|ClassName: AopLoggingConsole.Calculator, Method: Add, Args: 1, 2, Return type: System.Int32, Return value: 3
07/19/2019 19:32:36|Error|Exception|ClassName: AopLoggingConsole.Calculator, Method: Divide, Args: 12, 0, Exception: System.DivideByZeroException: Attempted to divide by zero.
   at AopLoggingConsole.Calculator.Divide(Int32 a, Int32 b) in /Users/otto/Source/GitHub/aop_logging/AopLoggingConsole/Calculator.cs:line 12

Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
   at AopLogging.LoggingProxy`1.Invoke(MethodInfo targetMethod, Object[] args) in /Users/ottogal/Work/SideProjects/GitHub/aop_logging/AopLogging/LoggingProxy.cs:line 34
--- End of stack trace from previous location where exception was thrown ---
   at System.Reflection.DispatchProxyGenerator.Invoke(Object[] args)
   at generatedProxy_1.Divide(Int32 , Int32 )
   at AopLoggingConsole.Program.Main(String[] args) in /Users/Otto/Source/GitHub/aop_logging/AopLoggingConsole/Program.cs:line 18

aop_logging's People

Contributors

dev-11 avatar

Watchers

 avatar

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.