0

I would like to detect if im logging just custom text or exception.

My loger look like this:

using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Xml.Serialization;
using Ultra_Script_WPF.User_Settings;

public class LogWriter
{
    private string m_exePath = string.Empty;
    public LogWriter(string logMessage)
    {
        LogWrite(logMessage);
    }
    public void LogWrite(string logMessage)
    {
        if (File.Exists("settings.xml"))
        {
            XmlSerializer xs = new XmlSerializer(typeof(Information));
            using (FileStream read = new FileStream("settings.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                Information info = (Information)xs.Deserialize(read);

                string EnableLogCBox = info.EnableLogging;
                bool? EnableLCBox = Convert.ToBoolean(EnableLogCBox);

                if (EnableLCBox == true)
                {
                    m_exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                    try
                    {
                        using (StreamWriter w = File.AppendText(m_exePath + "\\" + "log.txt"))
                        {
                            Log(logMessage, w);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }

            }
        }
    }

    public void Log(string logMessage, TextWriter txtWriter)
    {
        try
        {
            txtWriter.Write("\r\n");
            txtWriter.Write("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            txtWriter.Write("  : {0}", logMessage);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

When it logs exception it looks like this:


19:41:04 čtvrtek 26. března 2020  : Cryptography error (Mostly wrong password): System.Security.Cryptography.CryptographicException: Výplň je neplatná a nelze ji odebrat.
   v System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
   v System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
   v System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   v Ultra_Script_WPF.startup_password_prompt.OK_Button_Click(Object sender, RoutedEventArgs e) v C:\Users\Honza\Desktop\Prográmko\Ultra_Script\Projects\Ultra_Script_GIT\startup_password_prompt.xaml.cs:řádek 105

But i dont need the middle lines only want to log first and last one with error, location and line.

Can i somehow detect if im logging exception inside logwriter class itself? So i can make different log message for exception and for custom text

Johny Wave
  • 111
  • 2
  • 11
  • hmmm. I think, your line numbers etc are only available when you have PDB files. Are you expecting to have them in production? – T.S. Mar 26 '20 at 18:54
  • Maybe consider extracting some logic from LogWriter method and creating an overload that takes 2 parameters: string and Exception. Now in that overloaded method you already know it is called because of an exception so you can handle the text accordingly. – Jarek Danielak Mar 26 '20 at 18:56
  • @JarekDanielak great idea! il give it a try. – Johny Wave Mar 26 '20 at 19:01
  • @T.S. its showing lines in Release version – Johny Wave Mar 26 '20 at 19:01
  • Because you have PDB files [I think, this answers](https://stackoverflow.com/questions/3328990/how-can-i-get-the-line-number-which-threw-exception) – T.S. Mar 26 '20 at 19:34

0 Answers0