Yavs
Yavs

Reputation: 31

C# how to log file inside a DLL?

I'm developing a DLL and I want to log some data it generates.

I wanted to use "Log4Net", but I found the problem that in a DLL I don't have an "App.config" file where I can write the XML code, so I don't know how to implement this (I'm new in this matters).

I read about "Singleton" but I saw it's better to avoid it since it has it's issues (i.e hide some visibility of the code, problems with unit tests, ...).

So my question is: How and what is the best way to create a log file for the data generated by my DLL?

Upvotes: 2

Views: 6661

Answers (4)

sgmoore
sgmoore

Reputation: 16067

You don't say who you expect to use your dll.

If it will be used by lots of other people and if the logging is useful to them, then may not want to be forced to use log4net or this may cause problems if they want to use a different version of log4net than you are using.

I have seen several dlls which use Common.Logging to avoid this issue which allows the consumers to use whichever logging package they want.

Having said that, see Configure log4net logging in dll for another possible solution.

Upvotes: 0

Christopher
Christopher

Reputation: 9804

A DLL - a class library - should never be logging by itself. Even the ones that are there for output - like the one containing Console or even logger code - should never decide to write their own logfile. Logging work - all output work - that is not controllable or even fully controlled by the programmer using your DLL, is just going to be vexing behavior. And you should never write something with Vexing behavior.

Logging is the job of the person using your code, not of your code. If you are writing a Library or really anything else that usually has no output (like a Windows Service), it is customary to have a wrapper project for debugging and testing.

If it is important enough it warants an Exception. If it is not important enough for a Exception - it is propably not important enough at all. It is a daunting challenge to write good Exception handling, nevermind good Exception throwing code. But there are two articles on the mater that I link very often. And I really think would help you get you on the right paths:

They really helped me get a handle on it. And thus far they helped countless others. And their ideas are not even tied to .NET only.

Upvotes: 5

Gowthaman
Gowthaman

Reputation: 40

To log the application flow in the DLL, Just create a Class that create and access the log text file.

In that class, declare the object LoggingClass loggingObject; and then use this instance to access the log file.

In creating object for it, you can use,

public static LoggingClass createOrGetObject()
{
  return (loggingObject == null)? new LoggingClass() : loggingObject;
}

Now, just you can call this method to get the same instance that access the log file to write the log. In this example, Log4Net is not used but works fine for logging.

Upvotes: 0

Ygalbel
Ygalbel

Reputation: 5519

The config file will be connected in running module. It will be in exe file if it's a console application, or in web.config in case of web application.

Upvotes: 0

Related Questions