我可以用这段代码知道文件存储在哪里?并且日志代码是可行的吗? [英] May I know with this code where will the files be stored to? and is the logging code workable?

查看:49
本文介绍了我可以用这段代码知道文件存储在哪里?并且日志代码是可行的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以通过此代码知道文件存储在哪里?并且日志代码是可行的吗?谢谢你的帮助:)



May I know with this code where will the files be stored to? and is the logging code workable? Thanks for your help:)

public void log(String message)
{

    String oFileName = "Log_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".txt";
    if (!File.Exists(oFileName))
    {
        System.IO.FileStream f = File.Create(oFileName);
        f.Close();
    }

    try
    {
        System.IO.StreamWriter writter = File.AppendText(oFileName);
        writter.WriteLine(datetime.ToString("dd-MM-yyyy_hh-mm-ss") + " > " + message);
        writter.Flush();
        writter.Close();
    }
    catch (SqlException ex)
    {
        Console.WriteLine("SQL Error: " + ex.Message);
    }

    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
    }
}

推荐答案

只有文件名,没有提供目录路径。所以你的日志文件将创建你的exe文件执行的相同位置。

我想知道为什么你的代码中有SqlException catch,在给定的代码块中没有与SQL相关的代码。因此没有必要使用SqlException catch块。

除了你的代码似乎没问题。首先尝试自己,然后检查在exe文件所在的同一位置创建的日志文件。
There is only the file name, no directory path provided. so your log file will create same place where your exe file executing.
I wonder why there is SqlException catch in your code, there is no code related to SQL in the given code block. So there is no point of having SqlException catch block.
apart from that your code seems to be OK. just try yourself first and check the log files created in the same location where your exe file located.


不,从这段代码中,您不知道文件的确切位置。它是工作目录(也称为当前目录),此目录取决于启动应用程序的位置。如果只是单击.exe文件,它等同于cd命令wiuth可执行文件所在的目录,后面是可执行文件名作为下一个命令,它启动应用程序。因此,在这种情况下,工作目录将与可执行目录相同。一般情况下情况并非如此;您可以从任何其他目录启动相同的可执行文件。对于当前用户来说,这样的目录甚至可以是只读的,这会使这种日志记录变得不可能。



这是天真的日志记录,它可以帮助你简单的情况,但不够健壮,不够实用。建议的日志记录方式是使用 System.Diagnostics.EventLog

https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110 %29.aspx [ ^ ]。



这是一个完整的日志记录系统,除了默认记录到系统外,它还允许您事件日志,创建自己的自定义接收器以记录数据,或根据应用程序在系统日志中对记录信息进行分类。请查看我过去关于日志记录定制的答案:

在Windows应用程序中随时随地将TextBuild输出到TextBox [ ^ ],

如何在文件夹下创建事件日志 [ ^ ]。



另一种推荐的.NET应用程序登录方式是使用Apache log4net开源: https://logging.apache.org/log4net [ ^ ]。



-SA
No, from this fragment of code, you don't know exact location of the file. It is "working directory" (also called "current directory"), and this directory depends on where you start the application. If you simply click on the .exe file, it is equivalent to "cd" command wiuth the directory where the executable file is located, followed by the executable file name as the next command, which starts the application. So, in this case, working directory would be the same as executable directory. This is not so in general case; you can start the same executable from any other directory. Such directory can even be read-only for the current user, which would make this kind of logging impossible.

This is "naive" logging which can help you in some simple cases, but is not robust enough to be practical. Recommended way of logging is through the use System.Diagnostics.EventLog:
https://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog%28v=vs.110%29.aspx[^].

This is a whole logging system, which allows you, in addition to default logging to the system event log, create your own custom sinks for logging data, or classify your logging information in the system log according to the application. Please see my past answers on logging customization:
MsBuild OutPut to the TextBox on the fly in Windows Application[^],
How to create event log under a folder[^].

Another recommended way of logging in .NET applications would be open-source using Apache log4net: https://logging.apache.org/log4net[^].

—SA


这篇关于我可以用这段代码知道文件存储在哪里?并且日志代码是可行的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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