0

I have MyAwesomeProject which imports MyAwesomeLogger and calls it:

using MyAwesomeLogger;
using System.Web.Mvc;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var Logger = new LogFactory.GetLogger();
        Logger.Debug("Hello World");

        return View();
    }
}

LogFactory.GetLogger() method is inside MyAwesomeLogger and part of all the wonderful things it does, is sets the file name of the logger. My main problem is that I need to grab the project name and set that as the logging file name and this has to work for both console applications and web applications.

What I tried to do before was: Assembly.GetEntryAssembly().GetName().Name; While this works for console applications, it does not works for web. I've gone through a lot of links, but most of the solutions show how to get the project name from mvc project itself, not from a dll like this. What is the simplest fool proof way to do this?

Another thing I tried was:

var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
while (type != null && type.Namespace == "ASP")
{
    type = type.BaseType;
}

var fileName = type.Assembly.GetName().Name;

The above works if I call it in the HomeController, but MyAwesomeLogger doesn't know about HttpContext so I can't call it there.

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • Note that project name is not persisted anywhere in assembly - so you may have hard time trying to get one that way... – Alexei Levenkov May 03 '16 at 15:19
  • You may try Assembly.GetExecutingAssembly( ). But this may yield to strange results (it matters where you call it, if you call it in a cshtml you may get the dll for the precompiled page etc.) – apr May 03 '16 at 15:38
  • You may also try Assembly.GetCallingAssembly( ) ? [link](http://stackoverflow.com/questions/5478814/c-sharp-get-calling-methods-assembly) – apr May 03 '16 at 15:45
  • @apr I have tried CallingAssembly in the past but the problem is the result is then the name of my LoggingDll, not calling mvc project. – Bagzli May 03 '16 at 15:50
  • Your logger does know about HttpContext, but is trying to access it before the Http Request has initialized it. You can use a global asax file to subscribe to begin request and initialize your logger there. Or create an HttpModule and subscribe to begin request and initialize your logger there. – Ryan Mann May 03 '16 at 17:50

1 Answers1

0

A project's name isn't necessarily in the project's dll. A Project's DLL can be any name, it doesn't have to match the name of the project file in visual studio. So that is generally a bad way to go about getting the project name.

You can set Assembly Information on your DLL in project settings on the Application Tab.

The Title will be the name of the dll "XYZ" would be "XYZ.dll" when compiled. However you can also set a Description, Company, Product, and more.

You could use the Product Field as your project name and set it to the name of your Product.

To retrieve this information you should use a Type in your project to get an assembly. In a web app the entry point won't necessarily be your site's dll it could be a razor view engine, or aspx view engine, etc etc.

Type myType = typeof(ThingInMyProject);
Assembly myAsm = myType.GetAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(myAsm.Location);
var title = fvi.Title;
var companyName = fvi.CompanyName;
var productName = fvi.ProductName;

Then you can use the Title "Name of the Dll" or product name as you see fit.

Ryan Mann
  • 5,178
  • 32
  • 42
  • what is `typeof(ThingInMyProject)` ? I'm not sure what class you are trying to refer here. – Bagzli May 03 '16 at 16:31
  • Pseudo code... for you it would be typeof(HomeController), or any other type in your dll. – Ryan Mann May 03 '16 at 16:35
  • the problem is I don't know if its going to be HomeController or some function from a console app. This is an external dll that is being called by everything. – Bagzli May 03 '16 at 17:46
  • Is the logging code different for each dll or something? If the logging code is in the assembly with HomeController, then using HomeController just ensures you are getting the right assembly with the easiest way possible. In which case it will work. If the logging code is in each assembly and changes, then you should give your logger some kind of static property to define it and use reflection to find it. – Ryan Mann May 03 '16 at 17:48
  • If you post your MyAwesomeLogger code we might be able to better help you. – Ryan Mann May 03 '16 at 17:51