简体   繁体   中英

How to get the current project name in C# code?

I want to send an email to myself when an exception is thrown. Using StackFrame object, I am able to get File Name, Class Name and even class method that throw the Exception, but I also need to know the project name as many of my ASP.NET project has the same file name, class name and method.

This is my code:

    public static string JndGetEmailTextForDebuggingExceptionError(this Exception Ex)
    {
        StackFrame sf = Ex.JndGetStackFrame();

        string OutputHTML =         "<i><b><u>For Developer Use Only: </u></b></i>"                                                 + "<br>" + 
                                                                                                                                      "<br>" +
                                    "Project Name:   "  + HttpContext.Current.ApplicationInstance.GetType().Assembly.GetName().Name + "<br>" + //Under discussion
                                    "File Name:      "  + sf.GetFileName()                                                          + "<br>" +
                                    "Class Name:     "  + sf.GetMethod().DeclaringType                                              + "<br>" +
                                    "Method Name:    "  + sf.GetMethod()                                                            + "<br>" +
                                    "Line Number:    "  + sf.GetFileLineNumber()                                                    + "<br>" +
                                    "Line Column:    "  + sf.GetFileColumnNumber()                                                  + "<br>" +
                                    "Error Message:  "  + Ex.Message                                                                + "<br>" +
                                    "Inner Message : "  + Ex.InnerException.Message                                                 + "<br>";

        return OutputHTML;
    }

Thanks ALL.

You can use Assembly.GetCallingAssembly if you have your logging code in a separate library assembly, and call directly from your ASP.NET assembly to your library, and you mark the method so that it won't be inlined:

[MethodImpl(MethodImplOptions.NoInlining)]
public static string JndGetEmailTextForDebuggingExceptionError(this Exception Ex)
{
    StackFrame sf = Ex.JndGetStackFrame();

    string OutputHTML =         "<i><b><u>For Developer Use Only: </u></b></i>"                    + "<br>" + 
                                                                                                     "<br>" +
                                "Project Name:   "  + Assembly.GetCallingAssembly().GetName().Name + "<br>" +
                                "File Name:      "  + sf.GetFileName()                             + "<br>" +
                                "Class Name:     "  + sf.GetMethod().DeclaringType                 + "<br>" +
                                "Method Name:    "  + sf.GetMethod()                               + "<br>" +
                                "Line Number:    "  + sf.GetFileLineNumber()                       + "<br>" +
                                "Line Column:    "  + sf.GetFileColumnNumber()                     + "<br>" +
                                "Error Message:  "  + Ex.Message                                   + "<br>" +
                                "Inner Message : "  + Ex.InnerException.Message                    + "<br>";

    return OutputHTML;
}

On any entry points in your library that can end up wanting to log the project name, you'd have to record the calling assembly and mark it NoInlining , then pass that around internally.

If you're using .NET 4.5, there's an alternative way to do this: CallerFilePath . It has the same restrictions on entry points, and it returns the source path on your machine instead of the assembly name (which is probably less useful), but it's easier to know that it'll work (because it compiles it, just like optional parameters are compiled in), and it allows inlining:

public static string JndGetEmailTextForDebuggingExceptionError
              (this Exception Ex, [CallerFilePath] string filePath = "")
{
    StackFrame sf = Ex.JndGetStackFrame();

    string OutputHTML =         "<i><b><u>For Developer Use Only: </u></b></i>" + "<br><br>" +
                                "Source File Path:   "  + filePath + "<br>" +
...

For anyone looking for an ASP.NET Core compatible solution, the following should work;

System.Reflection.Assembly.GetEntryAssembly().GetName().Name


.NET Core API Reference

You can use

HttpContext.Current.ApplicationInstance.GetType().Assembly.GetName().Name

If this returns App_global.asax.... , change it to

HttpContext.Current.ApplicationInstance.GetType().BaseType.Assembly.GetName().Name

If you aren't running in an HTTP request, you will need some way to get ahold of the HttpContext .

This will return different results if you're in a Web Site project (as opposed to Web Application).

You can also use HttpRuntime.AppDomainAppPath to get the physical path on disk to the deployed site; this will work everywhere.

Reflection is the best way to find out product name,company name version like information.

public static string ProductName
{
 get
 {
    AssemblyProductAttribute myProduct =(AssemblyProductAttribute)AssemblyProductAttribute.GetCustomAttribute(Assembly.GetExecutingAssembly(),
     typeof(AssemblyProductAttribute));
      return myProduct.Product;
  }
}

And Another way like get direct project name

string projectName=System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();

This ought to be enough

string projectName = Assembly.GetCallingAssembly().GetName().Name;

Edit* If you are running this from another assembly then you should use GetCallingAssembly instead.

Just adding my answer here.

For those who work with multiple Dlls in their projects, the correct answer is:

string projectName = Assembly.GetExecutingAssembly().FullName.Split(',')[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM