I'm writing a Windows CE App and want to implement error logging to a .txt file.
I've done research and saw this example:
public static void Logs(string fileName, string methodName, string message)
{
try
{
if (!string.IsNullOrEmpty(message))
{
using (FileStream file = new FileStream(Application.StartupPath + "\\Log.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
StreamWriter streamWriter = new StreamWriter(file);
streamWriter.WriteLine((((System.DateTime.Now + " - ") + fileName + " - ") + methodName + " - ") + message);
streamWriter.Close();
}
}
}
catch
{
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
int a = 10, b = 0;
int result = a / b; //runtime Exception will throw
}
catch (Exception ex)
{
Logs(this.GetType().Name, "button1_Click()", ex.Message.ToString());
}
}
That works but only in Windows Forms. I'm getting this error in Windows CE:
'System.Windows.Application' does not contain a definition for 'StartupPath'
Ok, so I know I have to use this:
Path.GetDirectoryName (Assembly.GetExecutingAssembly ().GetName ().CodeBase);
But I'm not sure how. Still learning so please don't mark this as duplicate. Can someone rather just explain to me exactly where and how to use this in Windows CE app?
Thanks in advance.
You can write a function that returns the path of the executing assembly... just like:
public static string GetCurrentApplicationPath()
{
return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
and use this function in place of Application.StartupPath
Related
I have an event trigger that is meant to write to a .txt file within my resource folder. However, nothing is being written to said resource folder. I have no idea why this will not work and would appreciate any help.
private void button1_Click(object sender, EventArgs e)
{
int b = numericUpDown1.GetHashCode();
int c = numericUpDown2.GetHashCode();
int d = numericUpDown3.GetHashCode();
try
{
StreamWriter sw = new StreamWriter("orders.txt");
sw.WriteLine("Burger(s) " + b);
sw.WriteLine("Chip(s): " + c);
sw.WriteLine("Drink(s) " + d);
sw.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
Update:
This problem has been resolved. The code works, I was not aware that the .txt file was inside my debug folder.
You can use this method:
class WriteAllLines
{
public static async Task ExampleAsync()
{
string[] lines =
{
"First line", "Second line", "Third line"
};
await File.WriteAllLinesAsync("WriteLines.txt", lines);
}
}
from microsoft
Here
I am wondering on how to create a new directory for a log event file in windows service through c#
I have the following code:
public static void WriteLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\DataFile.txt", true);
//sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "C:\\MulpuriServiceLOG\\data.txt", true);
//sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
sw.WriteLine(Message);
sw.Flush();
sw.Close();
}
catch{}
}
As the docuemtation states for the Directory.CreateDirectory(path):
Creates all directories and subdirectories in the specified path unless they already exist.
Modified from the example source code:
string path = #"c:\MyDir";
try
{
// Try to create the directory.
Directory.CreateDirectory(path);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
There is a really great tutorial on dotnetperls containing example code, exceptions, tips and other useful information about creating directories!
Look up that SO-question to create folders inside the same directory your executable has started, or simple use relative paths instead of absolute ones:
Directory.CreateDirectory("Test");
That way you will never have conflicts about finding the correct path!
File yourFolder= new File("C:/yourFolder");
// if the directory does not exist, create it
if (!yourFolder.exists()) {
System.out.println("Creando directorio: " + yourFolder.getName());
boolean result = false;
try
{
yourFolder.mkdir();
result = true;
}
catch(SecurityException se){
}
if(result) {
System.out.println("Folder created");
}
}
Actually, I want to log the data in such a way that it should have the methods that the application goes through in c#, and if there is an error then the error content also should be logged. the problem is where to call the log methods inside of catch or inside of every method? as I have nearly 200 methods.
I wrote the code like this:
public static bool Logging(System.Reflection.MethodBase methodInfo)
{
var fullMethodName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
if (error_flag == false)
{
using (StreamWriter outputFile = new StreamWriter(path + #"\log.txt", true))
{
outputFile.WriteLine(fullMethodName + ": OK");
}
}
else
{
using (StreamWriter outputFile = new StreamWriter(path + #"\log.txt", true))
{
outputFile.WriteLine("\n\n --> Error in : " + fullMethodName);
}
}
return true;
}
//Logging Method 2
public static bool WriteErrorLog(Exception ErrorMessage)
{
using (StreamWriter outputFile = new StreamWriter(path + #"\log.txt", true))
{
outputFile.WriteLine("{0} Exception caught.", ErrorMessage);
}
return true;
}
and I have to call those methods from where??
I would suggest the following approach:
static void Main()
{
try
{
Log.Info("Start of process");
string input = StepOne();
StepTwo(input);
Log.Info("End of process");
}
catch(Exception e)
{
Log.Error(e);
}
}
public static string StepOne()
{
Log.Info("StepOne Completed");
return "Step One";
}
public static void StepTwo(string input)
{
Log.Info("StepTwo, input: " + input);
throw new ArgumentNullException("input");
}
Rather than rolling your own, use an existing logging framework, for example Log4Net.
Add a try catch at the highest possible layer, let errors bubble up until you can actually do something sensible with them.
Add logging messages to functions where it is sensible to do so, this will be the most useful information, for example you can log method inputs.
Avoid using reflection to get the method name, you probably don't need to log individual method names. All of that information will be in the stack trace anyway.
I'm quite a noob at programming and I've been stuck at this for a while now. I'm using the following code to get continuous data output streamed to a command prompt. How can I ensure that the output gets copied to a text file after closing the prompt manually?
public static void Main(string[] args)
{
Connector connector;
Console.WriteLine("HelloEEG!");
// Initialize a new Connector and add event handlers
connector = new Connector();
connector.DeviceConnected += new EventHandler(OnDeviceConnected);
connector.DeviceConnectFail += new EventHandler(OnDeviceFail);
connector.DeviceValidating += new EventHandler(OnDeviceValidating);
// Scan for devices across COM ports
// The COM port named will be the first COM port that is checked.
connector.ConnectScan("COM40");
// Blink detection needs to be manually turned on
connector.setBlinkDetectionEnabled(true);
Thread.Sleep(400000);
System.Console.WriteLine("Goodbye.");
connector.Close();
Environment.Exit(0);
}
// Called when a device is connected
static void OnDeviceConnected(object sender, EventArgs e)
{
Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;
Console.WriteLine("Device found on: " + de.Device.PortName);
de.Device.DataReceived += new EventHandler(OnDataReceived);
}
// Called when scanning fails
static void OnDeviceFail(object sender, EventArgs e)
{
Console.WriteLine("No devices found! :(");
}
// Called when each port is being validated
static void OnDeviceValidating(object sender, EventArgs e)
{
Console.WriteLine("Validating: ");
}
// Called when data is received from a device
static void OnDataReceived(object sender, EventArgs e)
{
Device.DataEventArgs de = (Device.DataEventArgs)e;
DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
/* Loops through the newly parsed data of the connected headset*/
// The comments below indicate and can be used to print out the different data outputs.
for (int i = 0; i < tgParser.ParsedData.Length; i++)
{
//string temp = tgParser.ParsedData[1].ToString;
//Console.WriteLine(tgParser.ParsedData.Length + " + " + temp);
if (tgParser.ParsedData[i].ContainsKey("Raw"))
{
//Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
//Console.WriteLine("Raw Value:" + tgParser.ParsedData[i]["Raw"]);
}
if (tgParser.ParsedData[i].ContainsKey("PoorSignal"))
{
//The following line prints the Time associated with the parsed data
//Console.WriteLine("Time:" + tgParser.ParsedData[i]["Time"]);
Console.WriteLine("Time:" + tgParser.ParsedData[i]["Time"]);
//A Poor Signal value of 0 indicates that your headset is fitting properly
Console.WriteLine("Poor Signal:" + tgParser.ParsedData[i]["PoorSignal"]);
poorSig = (byte)tgParser.ParsedData[i]["PoorSignal"];
}
if (tgParser.ParsedData[i].ContainsKey("Attention"))
{
//Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
Console.WriteLine("Att Value:" + tgParser.ParsedData[i]["Attention"]);
}
if (tgParser.ParsedData[i].ContainsKey("Meditation"))
{
//Console.WriteLine("Med Value:" + tgParser.ParsedData[i]["Meditation"]);
Console.WriteLine("Med Value:" + tgParser.ParsedData[i]["Meditation"]);
}
if (tgParser.ParsedData[i].ContainsKey("EegPowerDelta"))
{
//Console.WriteLine("Delta: " + tgParser.ParsedData[i]["EegPowerDelta"]);
Console.WriteLine("Delta: " + tgParser.ParsedData[i]["EegPowerDelta"]);
}
if (tgParser.ParsedData[i].ContainsKey("BlinkStrength"))
{
//Console.WriteLine("Eyeblink " + tgParser.ParsedData[i]["BlinkStrength"]);
Console.WriteLine("Eyeblink " + tgParser.ParsedData[i]["BlinkStrength"]);
}
}
}
It will be much better to log every console output to a file as it happens. Instead of waiting to write to file when the app is closed manually. To save yourself a lot of coding, you can use log4net to handle the logging.
There's several different ways of approaching this, and with a bit of research I'm sure you could find a few, however this is the solution I would use for this particular action :
As Jonesy mentioned in the comments, I would firstly tidy up your Main. Create a separate class to perform the console writeline and the text output at the same time.
In this class perhaps use a loop to output the data to a file as and when it happens, therefore you wouldn't have to code in the logic when the console is closed manually, which in turn would cover unexpected errors and loss of logs.
This might work.
public static void WriteToFileAndConsole()
{
string outFile = "ConsoleOut.txt";
using (FileStream fileStream = new FileStream(outFile, FileMode.OpenOrCreate))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
using (TextWriter originalConsoleOut = Console.Out)
{
Console.SetOut(writer);
Console.WriteLine("Hello To File");
Console.SetOut(originalConsoleOut);
}
}
}
Console.WriteLine("Hello to console only");
}
I am trying to add some logging functionality to a service, however I want it to create a new log, copy the old log into the new one and delete the original.
Sudo for ClearLog: if log1 full create log2, delete contents of log1
Please see code below, currently the ClearLog function is not doing what I want.
Can anyone see what I'm doing wrong?
public static void WriteLog(string txt)
{
string fp = _AppPath + #"\Logging";
try
{
File.AppendAllText(fp + #"\Log1.txt", txt);
}
catch (IOException iex)
{
Debug.Print("Error writing log" + iex.ToString());
}
}
private static void ClearLog()
{
string fp = _AppPath + #"\Logging";
try
{
if (!File.Exists(fp + #"\Log1.txt"))
{
WriteErrorLog("");
}
else
{
File.AppendAllText(fp + #"\Log1.txt", fp + #"\Log2.txt");
File.Delete(fp + #"\Log1.txt");
WriteLog("");
}
}
catch (Exception ex)
{
WriteLog("Clear log failed " + ex.ToString());
}
}
Try creating a public static field of the file path, as you're opening the same file twice it seems.
IE: public static string logfp = _AppPath + #"\Logging";
Then rename everything in those two functions logfp.
Improved example (can use paths in both or declared throughout)
private static void ClearLog()
{
string logfp = _AppPath + #"\Logging";
try
{
if (File.Exists(logfp + #"\Log2.txt"))
{
File.Delete(logfp + #"\Log2.txt");
if (File.Exists(logfp + #"\Log1.txt"))
{
File.Copy(logfp + #"\Log1.txt", logfp + #"\Log2.txt");
File.Delete(logfp + #"\Log1.txt");
}
else
{
File.AppendAllText(logfp + #"\Log1.txt", "New Log created: " + DateTime.Now.ToString());//showing you when it was created
}
}
else
{
File.Copy(logfp + #"\Log1.txt", logfp + #"\Log2.txt");
File.Delete(logfp + #"\Log1.txt");
}
}
catch (Exception ex)
{
WriteErrorLog("Clear log failed " + ex.ToString());
}
}