Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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());
    }
}
Posted

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.
 
Share this answer
 
v2
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900