在ASP.NET中创建错误日志 [英] Creating Error Log in ASP.NET

查看:68
本文介绍了在ASP.NET中创建错误日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在ASP.NET应用程序中创建错误日志.如果我将应用程序托管在开发计算机的IIS中,则这些代码将起作用.但是,如果我将其托管在另一个IIS中,则不会创建错误日志.有人请帮我

I am using the following codes for creating error log in an ASP.NET application. Those codes are working if I host the application in the IIS of the development machine. But, if I host it in another IIS, the error log is not created. Someone please help me

public void WriteError([System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")]  
string ErrorNumber, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorMessage, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorSource, [System.Runtime.InteropServices.OptionalAttribute, System.Runtime.InteropServices.DefaultParameterValueAttribute("")] 
string ErrorStackTrace)


{


{

string path = "~/Error/" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
}

using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
{
    w.Write("@" + "Log Entry : ");
    w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString());
    w.WriteLine("Error# :{0}", ErrorNumber);
    w.WriteLine("Message :{0}", ErrorMessage);
    w.WriteLine("Source :{0}", ErrorSource);
    w.WriteLine("StackTrace :{0}", ErrorStackTrace);
    w.Flush();
    w.Close();
}


}

推荐答案

您可能需要为该文件夹启用写权限.
You probably need to enable write permissions for the folder in question.


如下所示:
文件路径不可访问或文件夹不具有完全安全权限,因此无法创建日志文件.

如果已明确创建日志文件,则不提供写权限.

提供适当的安全权限,此问题应得到解决.
Looks like:
File path is not accessible or the folder does not has full security rights in order to create the log file.

In case log file has been created explicitly, the write permissions are not provided.

Provide the proper security permissions and the issue should get resolved.


假设参数正确传递,代码对我来说似乎还可以. 您可以编辑问题以包括方法签名吗(我在问这个是有原因的..)
The code seems okay to me assuming parameters are passed correctly.
Can you edit the question to include the method signature (There is a reason I am asking this.. )


这篇关于在ASP.NET中创建错误日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆