Using a log wrapper - c#

I wanted to know if you suggest (in your opinion) using / building a logger wrapper such as Common.Logging in order to use some kind of logger?
I presume that the use of such wrapper is to :
1. Enable changing the logger library when needed.
2. Make the API easier.
In the end does it really matters ? or the loggers are easy to use and do not need to simplify it or change the logger just like that.
Thanks.

I have written a wrapper logging library that uses health monitoring for asp.net sites and log4net for windows/console apps. This way the logging interface always stays the same and is easy to be used enterprise wide. In our case, we only wanted to log a few categories, error/debug/info etc. If you think you need to use some special features of a specific logging library, you may choose to implement it directly. If you are using any third party logging libraries, it may be good to have a wrapper because what if the support for that is stopped or you need to move to a better one later.
The decisions are always almost dependent on your specific needs and future plans.

I wouldn't do this without a good specific reason to do it.
I think that it's not very likely you are going to want to change the logging library, unless you chose badly in the start.
What you probably would want is to use features specific to the logger you chose, but that means the wrapper is not really useful.

Related

How to wrap each function in project?

I have tons of functions in my WinForms project and i'm using NLog for logging. I want to have an ability to wrap each function with nlogger.Info("start") and nlogger.Info("end") so i will know in which place actual exception occurred or where the code currently runs.
Is there a smart way to do it or i will need to place lines from above in all of my functions ?
I believe that PostSharp can do this for you, and can apply at the assembly level (via assembly-level attributes). However, I would be cautious of the potential performance impact. Writing the "aspect" should be pretty minimal.
Ifyou don't want to do it manually - you could consider Aspect Oriented Programming.
To summarise what it is, it matches functions which run and offers 'advice' before, after or around. So you can easily make one which runs at all functions and adds to the log. Never used it for C# but I used it for Java.
You can find a class for .NET. Read this for more info:
http://www.developerfusion.com/article/5307/aspect-oriented-programming-using-net/

Keeping a library dependency-free

I have a small Utility library containing some useful utility methods which have been fully unit-tested. At the moment, my library has no external dependencies. I am toying with the idea of adding logging to my classes which might be useful for debugging purposes. But this would mean bundling logging libraries along with my project.
My question is: should I keep my library dependency free? Are there any advantages of doing so?
I would add a logging interface that the can be used to abstract the logging. Then the allow users to add logging via this interface. You too should use this interface, and you should supply a 'NullLogger' built into your library that would be used if no other logging is needed.
You can make it easy to not use the NullLogger by asking users to configure a new one, simply by config file or by run time discovery.
Use Java Logging. It's part of JRE/JDK so no external libs are needed.
Check out examples.
There are many advantages of doing so, not the least the ability to run on most any operating system.
One way of keeping your library pretty dependency free, is to require it to initialized prior to use. Then you would in your_lib_init(); function take a function pointer to logging backend. This means, the backend can be rewritten for any platform it might run on.
Also figure out, if you want a library totally free of all library dependencies, or one that depends on the standard class path. If it is pure Java, it will run on J2ME, Android, native compiled Java with GCJ and what not. If it uses class path, it will be portable across all class path implementations, in practice wherever OpenJDK runs.
Pro:
Your library will be much smaller (chances are that you're only using a small part of the full functionality of any dependency)
No update hell (like your code needs library C, version 2, product X needs your code and library C, version 1).
You won't need to bend your spoon to the whims of someone else (say the library you goes from 1.x to 2.x -> you need to update your code)
Con:
Wasted code if product X also needs the library
How smart are you? Chances are that you can't match the thought, wisdom and time that already went into the library.
PS: If you want to support logging, add slf4j to your code; this is a 30KiB API which allows users of your code to use any logging framework out there. Do not use commons-logging.

Writing API in C# for My Application

I'll write a application but I've never experienced to allow people to use my application programming interface before.I mean how kinda design I should make to let people use my methods from outside world like API.
Please some one show me a way.I am kinda new to this.
Expose as little as you can. Every bit you publish, will return to you x100 in next version. Keeping compatibility is very hard.
Create abstractions for everything you publish. You will definitely change your internals, but your existing users should stay compatible.
Mark everything as internal. Even the main method of your application. Every single method that could be used, will be used.
Test your public API the same way you would for interfaces. Integration tests and so on. Note that your API will be used in unpredictable ways.
Maximize convention over configuration. This is required. Even if your API is a single method you will still need to support this. Just makes your life easier.
Sign, and strong name your assemblies, this is good practice.
Resolve as many FxCop and StyleCop errors as possible.
Check your API is compatible with the Naming Guidelines of your platform.
Provide as many examples as you can, and remember that most of the usage of your API will be Ctrl+C and Ctrl+V from these examples.
Try to provide documentation. Check that you do not have GhostDoc-style auto-generated documentation. Everybody hates this.
Include information on how to find you.
Do not bother with obfuscation. This will help you and your users.
ADDED
API should have as less dependencies as you can. For example, dependecies on the IoC containers should be prohibited. If your code uses it internally. Just ilmerge them into your assemblies.
It may not be the funniest reading, and certainly not the only reading to do on the subject, but while designing your class library (your API), do check in with the Design Guidelines for Developing Class Libraries every now and then, it's a good idea to have a design that corresponds a bit with the .NET Framework iteself.
Make your methods you want to expose to the outside world public.
I found this presentation to be particularly insightful:
How to Design a Good API and Why it Matters
http://lcsd05.cs.tamu.edu/slides/keynote.pdf
One way to do it is to create a DLL for your main functionality that others will use and an EXE that calls the methods in the DLL. If you want your application to support plug-ins, have a look at the System.AddIn namespace.
If you want to see what's new in this area, check out the Managed Extensibility Framework. It's a new/"unified (see the comments...)" method for exposing features for add-ins and other extensibility/modularity.

Programming Modular software

I stand in front of a little problem; I want to create a modular software.
Let's make it more general and not specific for my case. What I want to create is a software which loads dlls and those dlls adds features to the software.
Think of the dlls as xvid, divx or whatever additional codec, feature to your favorite video-player. I want a more custom modular program though, in my case I'm creating a software to handle Customers, Invoices and Products and different users might have different needs and therefore I need to handle this somehow!
However, re-compiling the software and specificly sending the new files to each different user is "stupid" I would rather create a modular software ( don't actually know if this is the correct term ).
So what I was thinking is that I begin creating a pattern of which my DLL's should follow.
Then I create a Module handler in my software which loads the actuall DLL and calls the method in it ( here's where the pattern come in! ).
What I'd like to know is; Am I on the right track?
Might you guys give me some pointers or examples on this matter?
This is all in C#, but would of course be interesting to see how it would differ in Java, Python or C++ too.
create a common interface IMyInterface for your classes which should include everything that is common between all of your Moduals. You should look into the Managed Extensibility Framework I believe you can get it from Codeplex.
You have to have a purpose. You either need the module to conform to some kind of interface or something the app can handle (like a method with a known attribute that you find via reflection). The interface then performs known functionality like invoice creation, invoice printing, etc.
Alternatively your app has to conform to some interface and uses hooks for the module to use to inject itself into your app.
Plugins would be good for something that can be easily sliced up (media codecs). Hooks would be good for something with a well-defined event model (like a web server). Which you use depends on how you want your modularity for customers, invoices, etc. to work.
Here is a similar SO thread. Here's a list of dependency injection frameworks, Microsoft's is Unity. Also, you can look at the Enterprise Library codebase to see how they implement their provider architecture, such as in the caching application block where you can plug in your own caching provider.

What is the point of using a Logging framework?

I think I might be missing the point of having a logging framework for your application. In all the small apps I've always written a small "Logging" class and just pass log messages to a method in it which is written to a file.
What is the purpose of a 3rd party logging framework like log4net? Is it thread safety with logging write operations or am I missing something?
That's an excellent question.
The first reason is "why not?" If you are using a logging framework, then you'll reap the maintainability benefits of using something already packaged.
The second reason is that logging is subtle. Different threads, sessions, classes and object instances may all come into play in logging, and you don't want to have to figure this problem out on the fly.
The third reason is that you may find a performance bottleneck in your code. Figuring out that your code is slow because you're writing to a file without buffering or your hard drive has run out of disk space because the logger doesn't rollover and compress old files can be a pain in the neck.
The fourth reason is that you may want to append to syslog, or write to a database, or to a socket, or to different files. Frameworks have this functionality built in.
But really, the first answer is the best one; there's very little benefit to writing your own, and a whole bunch of drawbacks.
you might want to switch to log to a db for some message, or an alerting system that pages the poor person on support
More importantly though, most logging frameworks allow you to specify the loglevel of different classes so you dont need to cut a new binary each time you want more/less logging (that verbose logging for the bug you just spotted in production)
Log4Net's site has more detail
Something the other comments have omitted: if there's already a library that does what you want, it saves you having to write the code.
Possibly you're playing semantics here: to me, a "logging framework" typically is little more than a class that writes log messages to a file... so what you've done is write your own logging framework. Given that you have done so, there is obviously some point in "using a Logging framework"!
Ultimately you're going to need to make sure it handles concurrent logging correctly (locking the output stream), can log to a file, syslog, etc., can do log rolling, and so on. You can save yourself that effort by using someone else's well-tested code.
In a word: flexibility. Log4xxx gives you the ability to do different logging levels, to log different modules of code to different files, and you can depend on it to be dependable no matter what strange situation it hits (what will your logger do if the disk is out of space?)
Logging frameworks offer flexibility and prevent you from reinventing the wheel. I know it's brain-dead simple to append to a file in any modern language, but does your home grown logger have multiple targets? Can you turn logging on and off at run-time? Why risk a flat tire when these wheels are available for free?
Depending on how smart your own logging system implementation is.
In java, if you want to inherit log types, etc., it might be too much hassle and you'd prefer a third-party tool like Log4J. I assume there are similar things for C#. Similarly if you want to determine log level from the command line.
If you just want to route all your System.out and control whether they are printed whenever you compile, your own logger would do just fine.
Just for an argument sake, why not a matured home grown? which doesn't come's with any extra baggage and you have complete control over it.
Most of the Logging frameworks has too many features which we really wont use most of them and having said that they comes with there own baggage(Its just not a class, its a framework). Why not implement a simple home grown logging where all your applications write there logs to a Queue and a simple offline service(may be a windows service) read the queue and writes it to your desired location(File, Database etc). by Using queue you can achieve async operation and no locking issues.

Categories