我能成功创建日志吗?我暂时不能尝试,建议谢谢 [英] Will I be able to create my log successfully? I cant try it for the time being do advise thanks

查看:79
本文介绍了我能成功创建日志吗?我暂时不能尝试,建议谢谢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace ExportDataTableApplication
{
    class ExportDataTable
    {
        DateTime datetime = DateTime.Now;
        //Execute stored procedure
        static void Main(string[] args)
        {
            Console.WriteLine("Running App");

            DataSet ds = new DataSet();
            DateTime datetime = DateTime.Now;

            using (SqlConnection conn = new SqlConnection("Test connection string"))
            {
                string filePath = @"C:\Error.txt";
                try
                {
                    SqlCommand sqlComm = new SqlCommand("Test", conn);

                    sqlComm.CommandType = CommandType.StoredProcedure;

                    SqlDataAdapter da = new SqlDataAdapter(sqlComm);

                    da.Fill(ds);
                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    using (StreamWriter writer = new StreamWriter(filePath, true))
                    {
                        writer.WriteLine("Date :" + DateTime.Now.ToString() + "=> Exception :" + e.Message + "<br/>" + Environment.NewLine + "StackTrace :" + e.StackTrace +
                           "" + Environment.NewLine);
                        writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
                    }
                }

推荐答案

使用自制的记录方法,您将面临许多问题。不要这样做。



我已经回答了你的其他问题,所以请使用我的建议:我可以用这段代码知道文件存储在哪里?并且日志代码是否可行? [ ^ ]。



-SA
With your home-baked logging approach, you will face many problems. Don't do it.

I already answered you other question, so please use my advice: May I know with this code where will the files be stored to? and is the logging code workable?[^].

—SA


这是可能的。

您必须创建对象并在该对象上调用该方法,如下所示:



LogObject o = new LogObject();

o.log(这是我的信息);







var myMessage =这是我的信息;

o.log(myMessage);



第二个选项是让方法保持静态



而且你可以像这样调用它:

LogObject .log(这是我的信息);



这是你想知道的吗?或者您正在寻找完全不同的东西?



评论后更新:



如果我说对了你试图把异常写入日志文件。

试试这个:



试试

{

//你的代码

}

catch(例外e)

{

LogObject .log(e.Message);

}



这就是你需要的吗?
It is possible.
You have to create object and call that method on that object like this:

LogObject o = new LogObject();
o.log("This is my message");

or

var myMessage = "This is my message";
o.log(myMessage);

Second option is make the method static

And than you can call it like this:
LogObject.log("This is my message");

Is this what you wanted to know? or are you looking for something completely different?

Updated after comment:

If I get it right you are trying to write exception into a log file.
try this:

try
{
//your code
}
catch(Exception e)
{
LogObject.log(e.Message);
}

Is this what you need?


这篇关于我能成功创建日志吗?我暂时不能尝试,建议谢谢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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