Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I was searching for a way to get all active log4net logfiles (if any) at runtime without adding a reference to the project.

This is the solution I came up with:

private static IEnumerable<AbstractReportInformation> GetLog4NetLogfiles()
{
    var type = Type.GetType("log4net.LogManager,log4net");
    if(null == type)
        return new List<AbstractReportInformation>();

    dynamic[] repositories = type.GetMethod("GetAllRepositories").Invoke(null, null) as dynamic[];

    IEnumerable<FileInfo> files = repositories
        .SelectMany<dynamic, dynamic>(repos => repos.GetAppenders())
        .Where(appender => Type.GetType("log4net.Appender.FileAppender,log4net").IsAssignableFrom(appender.GetType()))
        .Select(appender => new FileInfo(appender.File));

    return files.Select(
        file => new FilesystemInformation(file)).Cast<AbstractReportInformation>().ToList();
}

It makes use of the dynamic keyword instead of doing it all with reflection.

  • Are there any obvious errors in my code?
  • Are there better ways to achieve the same result?

I feel a bit unsure about leaving the safety of the otherwise strongly typed language / framework.

Update to explain why no references shall be used / why dynamic is used

The above code is used to "detect" whether log4net is present and then gathers the logfiles. The same will later be implemented for NLog and possibly other frameworks of that sort. The project I'm using this code in is a application health monitoring framework and I don't want to add a lot of references.

share|improve this question

1 Answer 1

Well, that seems a strange solution to me. Despite the fact you don't have a formal reference in References folder of a VS project you still have a dependency on the log4net library. And if in runtime the log4net library won't be available, your code will fail. Moreover, somebody has to remember about that dependency and copy log4net.dll manually to you bin folder, so the code could be executed.

From my point of view, it is always better to have a visible dependency rather than having invisible one. The only exception comming to my mind is when you need to have a modular architecture where modules can be safely plugged and unplugged. And, as far as I can understand, it is not your requirement.

I would recommend you to create a separate project that would have a normal reference to log4net and which would be used by your main project. In that case you will have your main project unaware of log4net, but in the same time you would have all compile-time checks and every benefit of strongly typed language / framework.

share|improve this answer
    
Thanks for your review. The above code is used to "detect" whether log4net is present and then gathers the logfiles. The same will later be implemented for NLog and possibly other frameworks of that sort. The project I'm using this code in is a application health monitoring framework and I don't want to add a lot of references. –  yas4891 Dec 30 '11 at 15:54
    
@yas it would be great if you could add that note to the original question ;-) it's a very good explanation of why using dynamic here makes sense. –  codesparkle Jan 1 '12 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.