我有一个C#ASP.net 网站。 在本地,我可以在调试中运行它并逐步完成代码以查看为什么事情不起作用,但是当它托管在我的实时站点上时,我无法执行此操作。
调试我的网站的最佳方法是什么?
我应该添加首次亮相/输出/跟踪语句吗?
如果是这样,我该如何查看这些输出?我可以以某种方式在Chrome-->开发者工具中查看它们吗?
例如,现在我可以在我的网站上注册一个用户,这样我知道数据库连接良好,但我无法登录注册用户并想找出原因。
谢谢
可以在应用上添加跟踪和调试日志。为方便起见,您可以使用日志记录框架,例如http://nlog-project.org/https://serilog.net/
您实际上可以编写自己的日志记录机制,您可以在其中创建一个日志类和其中的一些函数,例如
public class Log
{
internal static bool RecordLog(string strSource, string strMethodName, string strStatement)//additional params you think appropriate for your logs
{
List<string> lstInfo = new List<string>();
string strProductName = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location.ToString()).ProductName.ToString();
string strProductVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location.ToString()).ProductVersion.ToString();
try
{
strProductName = FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location.ToString()).ProductName.ToString();
strProductVersion = FileVersionInfo.GetVersionInfo(Assembly.GetCallingAssembly().Location.ToString()).ProductVersion.ToString();
}
catch
{
}
try
{
lstInfo.Add("** Date=" + DateTime.Now.ToString("d MMM yy, H:mm:ss") + ", " + strProductName + " v" + strProductVersion);
lstInfo.Add("Source=" + strSource + ", Server=" + strServerIP + ""); //add more info in list as per rquirement
bool flag = blnWriteLog("LogFilename", lstInfo);
}
catch (Exception objEx)
{
//exception handling
}
return true;
}
private static bool blnWriteLog(string strProductName, List<string> lstInfo)
{
string strPath = strGetLogFileName(strProductName);
using StreamReader write in the log file received
return true;
}
private static string strGetLogFileName(string strFilePrefix)
{
//logic to check your file name, if it exists return name else create one
return strFile;
}
}
然后你可以从你的文件中使用相同的文件
Log.RecordLog()// Values as per your code and requirement
注意:以上只是建议的方法,还可以有许多其他有效的方法
可以使用内置的 Microsoft 智能跟踪功能从生成的智能跟踪日志中单步执行代码。此链接 https://msdn.microsoft.com/en-us/library/dn449058.aspx 提供了有关如何实现以下内容的说明;
"如果您使用监视代理来控制 IntelliTrace Microsoft, 您还需要在 上设置设置应用程序性能监视 您的网络服务器。这会在应用运行时记录诊断事件 并将事件保存到智能跟踪日志文件中。然后,您可以查看 Visual Studio Enterprise 中的事件(但不是专业或 社区版),转到事件发生的代码,查看 该时间点记录的值,并向前移动或 向后浏览运行的代码。找到并修复 问题,重复该循环以生成、发布和监视您的发布 因此,您可以更早、更快地解决未来的潜在问题。