Forum > Coding >

[Source] Log Errors To File

Posts: 1167

Threads: 72

Joined: Oct, 2019

Reputation: 89

Posted

By Immoon

 

So I Was Bored And Saw In Exo's A Class Called Logger Basically Gets Logs And Logs It To A File Providing Better Details To Possibly Help Support Staff Etc So I Took This Idea And Remade In Nihon Just Making It Better Overall And Better Performing So I'm Releasing It Here Since I Gave To Minish And Can't Expect It Not To Be Shared So Here It Is Ofc I'm Not Gonna Post The Really Good One I Wrote This Will Be A Watered Down Version That Works Well If You Use Credit OwO

 

Steps

 

1. Make A Class Call It FileLogger Or Whatever The F You Wanna Call It

2. Paste The Code Below Fix Errors Add Needed Using Statements And Just Fix Errors

 

Heres A Example Of Usage

private void WindowLoaded(object sender, RoutedEventArgs e)
{
      Exception Ex = new Exception();
      Ex.Throw();
      Logger.Log(Ex.ToString(), FileLogger.LogTypes.Error);
}

Btw This May Be Written Wrong WeAreDevs Has No Syntax So Idk

   public class FileLogger
    {
        public enum LogTypes
        {
            Error,
            Warn,
            Success
        };

        public static string CurrentFileName = DateTime.Now.ToShortTimeString() + ".txt";

        public static string GetLastCreatedLog() { return Directory.GetCurrentDirectory() + "\\System Logs\\" + CurrentFileName; }

        public static void Log(string Text, LogTypes LogType)
        {
            string FilePath = Directory.GetCurrentDirectory() + "\\System Logs\\" + CurrentFileName.Replace(":", "-");
            string Message = "[" + DateTime.UtcNow.ToLocalTime().ToString() + "] : " + Text;

            try
            {
                if (!File.Exists(FilePath))
                {
                    using (StreamWriter StreamWriter = new(FilePath))
                    {
                        StreamWriter.WriteLine(Message);
                        StreamWriter.Close();
                    }
                }

                if (LogType == LogTypes.Error) 
                {
                        /* If Error Show Msg Box Or Some Thing */
                }
            }
            catch (Exception Ex)
            {
                Logger.Log(Ex.ToString(), LogTypes.Error);
            }
        }
    }

 

Credit Me You Skid :Gun: Or I Will Find You

  • 0

Moon

Moon

vip

Posts: 6166

Threads: 262

Joined: Aug, 2020

Reputation: 78

Replied

Vouch for Immune

 

 

😳

 

https://media.discordapp.net/attachments/769721958426083333/866511170201714768/image.png

  • 0

Posts: 150

Threads: 32

Joined: Apr, 2021

Reputation: 8

Replied

prolly not gonna use this but vouch for zaddy immoon 

  • 0

"r u alce" - pleb8000
"lets make stuff" - Immoon
https://cdn.discordapp.com/attachments/850904515858923590/921759578620825640/VortexAllias.png

MINISHXP

minishxp

Posts: 813

Threads: 5

Joined: Jan, 2021

Reputation: 9

Replied

libs.logger.Log("hehe boi");

vouch for improving my code :blush:

 

VOUCH

  • 0

hi123

halvik915

Talan2016

Posts: 197

Threads: 15

Joined: Feb, 2021

Reputation: 6

Replied

Vouch cool beans

  • 0

                                                                 https://cdn.discordapp.com/attachments/901227777741692968/991219346124853278/sJkFFl5.png

we love security breaches

Posts: 0

Threads: 0

Joined: ?

Reputation:

Replied

cool great cool cool awesome great cool great

  • 0

Posts: 1658

Threads: 10

Joined: Sep, 2020

Reputation: 59

Replied

  • 0

Discord : Doctor Doom#0550

NeonFox

Your real dad

Posts: 219

Threads: 11

Joined: Jun, 2020

Reputation: 2

Replied

vouch

Content length must be 69-420 chars

  • 0

use. dark. mode.

Posts: 673

Threads: 30

Joined: Aug, 2020

Reputation: 6

Replied

Exception.Message

This Exists, However, if you want a full StackTrace then don't mind me lol

 

StreamWriter.Dispose();
  • 0

Posts: 0

Threads: 0

Joined: ?

Reputation:

Replied

vouch /charrrsssssss

  • 0

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )