Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. create LogError file(C# Coding) [modified] for WindowsApplication

create LogError file(C# Coding) [modified] for WindowsApplication

Scheduled Pinned Locked Moved C#
questioncsharpxmlhelp
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jojoba2010
    wrote on last edited by
    #1

    C# Coding WindowsApplication :

    1. How can I have Log for error in try catch with this informations 1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred : 2) Where I save this log info. I mean shall i save them in XML or word File ?

    modified on Thursday, February 4, 2010 4:38 AM

    Z W P 3 Replies Last reply
    0
    • J jojoba2010

      C# Coding WindowsApplication :

      1. How can I have Log for error in try catch with this informations 1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred : 2) Where I save this log info. I mean shall i save them in XML or word File ?

      modified on Thursday, February 4, 2010 4:38 AM

      Z Offline
      Z Offline
      Zar Ni
      wrote on last edited by
      #2

      U may want to use log4net. It's quite simple. :) A Brief Introduction to the log4net logging library, using C#[^]

      J 1 Reply Last reply
      0
      • Z Zar Ni

        U may want to use log4net. It's quite simple. :) A Brief Introduction to the log4net logging library, using C#[^]

        J Offline
        J Offline
        jojoba2010
        wrote on last edited by
        #3

        I wanna this log for windowsApplication ?! So Please HELP !

        Z 1 Reply Last reply
        0
        • J jojoba2010

          I wanna this log for windowsApplication ?! So Please HELP !

          Z Offline
          Z Offline
          Zar Ni
          wrote on last edited by
          #4

          Configure Log4Net for Desktop and Web Applications[^]

          1 Reply Last reply
          0
          • J jojoba2010

            C# Coding WindowsApplication :

            1. How can I have Log for error in try catch with this informations 1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred : 2) Where I save this log info. I mean shall i save them in XML or word File ?

            modified on Thursday, February 4, 2010 4:38 AM

            W Offline
            W Offline
            Wamuti
            wrote on last edited by
            #5

            You are being helped. log4net is a tool to help the programmer output log statements to a variety of output targets. http://logging.apache.org/log4net/[^] If you want to re invent the wheel you can create a log file, for example, mylogger.logx using the StreamWriter class maybe like this:

            public void Log(string Err)
            {
            using (StreamWriter sw = new StreamWriter("log.txt", true))
            {
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("NEW LOG");
            sw.WriteLine("-------------------");
            sw.WriteLine();
            sw.WriteLine();
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("-------------------");
            sw.WriteLine();
            sw.WriteLine(Err);
            }
            }

            Then,

            jojoba2010 wrote:

            Error Name :

            assuming you caught the exception in the catch block like this

            catch (Exception ex)
            {
            Log(ex.Message.ToString());
            }

            jojoba2010 wrote:

            Form Name:

            Log(this.Text);

            jojoba2010 wrote:

            Function Name that error occurred :

            Log(ex.Source);

            jojoba2010 wrote:

            Date_Time when error occurred :

            The Log function can already do that. Read about Exception and see the many things you can know about an exception that has occured!

            Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

            J 1 Reply Last reply
            0
            • W Wamuti

              You are being helped. log4net is a tool to help the programmer output log statements to a variety of output targets. http://logging.apache.org/log4net/[^] If you want to re invent the wheel you can create a log file, for example, mylogger.logx using the StreamWriter class maybe like this:

              public void Log(string Err)
              {
              using (StreamWriter sw = new StreamWriter("log.txt", true))
              {
              sw.WriteLine();
              sw.WriteLine();
              sw.WriteLine("NEW LOG");
              sw.WriteLine("-------------------");
              sw.WriteLine();
              sw.WriteLine();
              sw.Write("The date is: ");
              sw.WriteLine(DateTime.Now);
              sw.WriteLine();
              sw.WriteLine();
              sw.WriteLine("-------------------");
              sw.WriteLine();
              sw.WriteLine(Err);
              }
              }

              Then,

              jojoba2010 wrote:

              Error Name :

              assuming you caught the exception in the catch block like this

              catch (Exception ex)
              {
              Log(ex.Message.ToString());
              }

              jojoba2010 wrote:

              Form Name:

              Log(this.Text);

              jojoba2010 wrote:

              Function Name that error occurred :

              Log(ex.Source);

              jojoba2010 wrote:

              Date_Time when error occurred :

              The Log function can already do that. Read about Exception and see the many things you can know about an exception that has occured!

              Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

              J Offline
              J Offline
              jojoba2010
              wrote on last edited by
              #6

              I want the function name where this error occurred in ! I mean in which this try and catch is ?

              L Z 2 Replies Last reply
              0
              • J jojoba2010

                I want the function name where this error occurred in ! I mean in which this try and catch is ?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Have a look at Exception.TargetSite then. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                [The QA section does it automatically now, I hope we soon get it on regular forums as well]


                1 Reply Last reply
                0
                • J jojoba2010

                  I want the function name where this error occurred in ! I mean in which this try and catch is ?

                  Z Offline
                  Z Offline
                  Zar Ni
                  wrote on last edited by
                  #8

                  u can get the current class and method name like this. Below as a class variable.

                  private static readonly string _className =
                  System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();

                  Put below code inside each function that u want to log.

                  string thisMethod = _className + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()";

                  then whenever u want to log the info, u can just simply use

                  logger.info("Error Occurred + thisMethod);

                  1 Reply Last reply
                  0
                  • J jojoba2010

                    C# Coding WindowsApplication :

                    1. How can I have Log for error in try catch with this informations 1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred : 2) Where I save this log info. I mean shall i save them in XML or word File ?

                    modified on Thursday, February 4, 2010 4:38 AM

                    P Offline
                    P Offline
                    Pr teek B h
                    wrote on last edited by
                    #9

                    jojoba2010 wrote:

                    1. Where I save this log info. I mean shall i save them in XML or word File ?

                    you can save the data anyways you want it. xml is structured and easier to browse through. text is easy to implement.

                    jojoba2010 wrote:

                    1. How can I have Log for error in try catch with this informations 1.Error Name : 2.Form Name: 3.Function Name that error occurred : 4.Date_Time when error occurred :

                    void AddLogEntry(String errorName, String formName, String functionName)
                    {
                    DateTime entryTime = DateTime.Now;

                    //depending on how you want to save the entry i.e. xml or plain text
                    //append the text in the log file with the entry
                    //search in google on how to add text to a text/xml file.
                    }

                    or, you can do this:

                    void AddLogEntry(Exception ex)
                    {
                    String errorName = ex.message;
                    String trace = ex.StackTrace; //stack trace will give you a brief history on why the error occured
                    DateTime entryTime = DateTime.Now;

                    //depending on how you want to save the entry i.e. xml or plain text
                    //append the text in the log file with the entry
                    //search in google on how to add text to a text/xml file.
                    }

                    hope this helps and good luck :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups