我试图从发起异常的类中获取Class和MethodName。请参阅以下代码:
[MethodImpl(MethodImplOptions.NoInlining)]
public void Log(object obj, LogTypes logType, int userId = 0, string userName = "", [CallerFilePath] string sourceFilePath = "",[CallerMemberName] string methodName = "")
{
var appLog = new ApplicationLog();
try
{
appLog.UserId = userId;
appLog.UserName = userName;
appLog.InstanceName = ConfigurationSettingsHelper.InstanceName;
appLog.ProjectName = HostingEnvironment.IsHosted ? HostingEnvironment.ApplicationHost.GetSiteName() : Assembly.GetCallingAssembly().GetName().Name;
appLog.LoggedOn = DateTime.UtcNow;
appLog.FilePath = sourceFilePath;
if (logType==LogTypes.Exception)
{
if (obj is Exception ex)
{
appLog.LogType = 1;
appLog.LogDetails = ex.ToString();
if (ex.TargetSite.DeclaringType != null)
appLog.ClassName = ex.TargetSite.DeclaringType.FullName;
appLog.MethodName = ex.TargetSite.Name;
appLog.Message = ex.Message;
}
}
else
{
appLog.LogType = 2;
var reflectedType = new StackFrame(1).GetMethod().ReflectedType;
if (reflectedType != null)
appLog.ClassName = reflectedType.FullName;
appLog.MethodName = methodName;
appLog.LogDetails = obj is string ? obj.ToString() : "NA-Not a valid logging.";
}
}
catch (Exception ex)
{
//In case of unhandled exceptions.Try logging internal exception once.
//If failed, pass by silently.
try
{
appLog = new ApplicationLog
{
UserId = userId,
UserName = userName,
FilePath = new StackFrame(1).GetFileName(),
Message = ex.Message,
InstanceName = ConfigurationSettingsHelper.InstanceName,
ProjectName = Assembly.GetCallingAssembly().GetName().Name,
LoggedOn = DateTime.UtcNow,
LogType = 1,
LogDetails = ex.ToString()
};
if (ex.TargetSite.DeclaringType != null)
appLog.ClassName = ex.TargetSite.DeclaringType.FullName;
appLog.MethodName = ex.TargetSite.Name;
}
finally
{
HttpWebMethods.PostAsync(ConfigurationSettingsHelper.LoggerApiBaseAddress,
ConfigurationSettingsHelper.SaveLogEndpoint, appLog);
} // intentionally eating exception}
}
finally
{
HttpWebMethods.PostAsync(ConfigurationSettingsHelper.LoggerApiBaseAddress,
ConfigurationSettingsHelper.SaveLogEndpoint, appLog);
}
}
但是上面的代码捕获了错误的类和方法名称。请参阅下面的例外情况。
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at Common.ConvertAIToPNG.DAMFileOperation(String fsGUIDFolder, Int32 fiUserID, String fsInputFileName, String& fsOutPutFileName)
in c:\Main\PDM\App_code\common\ConvertAIToPNG.cs:line 70
at DAMFilesUploader.UploadFile(HttpContext context) in c:\Main\PDM\Uploader\DAMFilesUploader.ashx:line 82
我正在捕获并记录此异常以及它发生的类名和方法名称。但我在这里
类:System.Number |方法:StringToNumber
然而,预期应该是 类:ConvertAIToPNG方法:DAMFileOperation
我知道,我正在重新发明轮子。我可以使用Log4Net / Elmah。但是,我仍想弄清楚原因/解决方案。 此外,我在网站而不是WebApp / Windows应用程序中使用此代码。。但我已经考虑了所有案例,并试图使其更通用。
先谢谢。
答案 0 :(得分:0)
谢谢,我得到了修复。这是一个简单的修复。
替换
<div class="card" *ngFor="let item of galleries;"
(mouseenter)=" setBackground(item?.image?.fullpath)"
(mouseover)="showCount = true; getImagesCount(item.path);" (mouseleave)="showCount = false">
<img class="card-img-top" [routerLink]="['/'+item.path]"
[src]="item?.image?.fullpath ? path + '0x156/' + item?.image?.fullpath : defaultImage"
alt="Card image cap">
<div class="card-block">
<h4 class="card-title imagesCount"></h4>
<h4 class="card-title title">{{item.name}}</h4>
<img class="closeIcon" src="../../../assets/images/close.png" alt="" (click)="removeGallery(item);">
<span class="animated fadeInUp" *ngIf="showCount">{{imagesCount}}</span>
</div>
</div>
用这个
if (ex.TargetSite.DeclaringType != null)
appLog.ClassName = ex.TargetSite.DeclaringType.FullName;
appLog.MethodName = ex.TargetSite.Name;