topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 8:16 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Hi, first post in the developers section  (Read 11426 times)

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Hi, first post in the developers section
« on: May 29, 2016, 11:37 AM »
I have some code to show, but I'm not sure if I should post it or not. It's not a product/project, but something I use all the time. It's a function that logs to a file. I also use it for debugging because it can tell you what method the error came from. It uses reflection. It's just for developers who might be using vb.net. But I'm sure you could take the code as a skeleton and use it in some other language.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,896
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #1 on: May 29, 2016, 11:40 AM »
Hi Jumper and welcome to the site.
Absolutely, share away!

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Thanks.
« Reply #2 on: May 29, 2016, 12:17 PM »
Here's what you need in the imports section:

Code: Visual Basic [Select]
  1. Imports System.IO
  2. Imports System.IO.File
  3. Imports System.Reflection

And here's the function. I imagine that some people will notice that I check to see if the file is there. Then in the try block, the first thing I do is try to delete the file. I can't really remember why I did this. It may have been just wanting to make sure it was not there before creating the new file.

Here's the function, also here's what's at the top of the class that defines logfilename
Code: Visual Basic [Select]
  1. Const logFilename As String = "logFile.txt"
  2.  
  3.     Public Function logFile(LogMessage As String, Optional ex As Exception = Nothing) As Boolean
  4.  
  5.         If Not Exists(logFilename) Then
  6.             Try
  7.                 Delete(logFilename)
  8.                 Create(logFilename).Dispose()
  9.  
  10.                 Using sw = AppendText(logFilename)
  11.                     sw.WriteLine(LogMessage)
  12.                 End Using
  13.                 Return True
  14.             Catch
  15.                 Throw New Exception("File Not Found " & ex.Message)
  16.             End Try
  17.         Else
  18.             Using sw = AppendText(logFilename)
  19.                 sw.WriteLine(LogMessage)
  20.             End Using
  21.         End If
  22.         Return False
  23.     End Function

And here's the way you do it if you want to know what function an error came from. Also ex.Message is optional.
Code: Visual Basic [Select]
  1. logFile(MethodBase.GetCurrentMethod.ToString & " Some Error Message " & ex.Message)

Hope someone finds this helpful. I almost forgot... when you define just the logfileName, it writes the information in the working directory. If you want to go somewhere else, just add the path to the const. And, the error text in the catch section should be changed to something else. I just didn't take the time to do it yet.

And... I made this a while ago, so it's not very OO.
« Last Edit: May 29, 2016, 02:46 PM by skwire »

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #3 on: May 29, 2016, 02:15 PM »
We have code highlighting format that can make it even more useful.  There's a drop-down above the post area that has formatting for many different languages.  Visual basic is there.

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #4 on: May 29, 2016, 02:25 PM »
Sorry, I didn't see that. I'll fix it.

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #5 on: May 29, 2016, 02:27 PM »
I hate to be dumb, but I don't see it.

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #6 on: May 29, 2016, 02:30 PM »
I don't know if this is what you meant. I just put some blocks in there That I've seen elsewhere.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #7 on: May 29, 2016, 02:47 PM »
I set the highlighting but it appears that the VB highlighter isn't working properly (or there was nothing to highlight in jumper's code).

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #8 on: May 29, 2016, 04:19 PM »
I set the highlighting but it appears that the VB highlighter isn't working properly (or there was nothing to highlight in jumper's code).

I couldn't find the source code "button" or whatever it is so I just put it in code brackets:

What am I supposed to be looking for? That would help. While I'm making this post, I'm looking for something that has to do with code, but I don't see anything, but I'm sure it's pilot error.

Also, I don't see where to select 'quote' either (I made the one I'm using)

We have code highlighting format that can make it even more useful.  There's a drop-down above the post area that has formatting for many different languages.  Visual basic is there.

There IS no pull down menu when I'm making a post.
« Last Edit: May 29, 2016, 04:26 PM by jumper »

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #9 on: May 29, 2016, 04:43 PM »
skwire, why don't you log in as me? So you can tell me if I'm losing my mind or what.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #10 on: May 29, 2016, 05:17 PM »
skwire, why don't you log in as me? So you can tell me if I'm losing my mind or what.

I can't log in as you but here's a screenshot of what we're talking about.

2016-05-29_171510.pngHi, first post in the developers section

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #11 on: May 29, 2016, 05:38 PM »
I can't log in as you but here's a screenshot of what we're talking about.

I was just about to tell you I don't see 'any' of that, but your post reminded me of something. I use No Script for Firefox, and that's the reason I couldn't see it. When I changed it to 'trust this site'... Everything showed. No wonder you thought I was nuts <smile>

I was beginning to think the same thing.

wraith808

  • Supporting Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 11,186
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #12 on: May 29, 2016, 08:54 PM »
Well, that's good!  The highlighting might not be working, but the code formatting is working, which is one thing.  I guess we should ping mouser about the highlighting.

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #13 on: May 29, 2016, 09:01 PM »
Well, that's good!  The highlighting might not be working, but the code formatting is working, which is one thing.  I guess we should ping mouser about the highlighting.

Yea, I'm glad I found out what was going on. I think it would be a good thing to let someone know, but I vote for you because I only joined today :cool:

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #14 on: May 31, 2016, 07:18 PM »
Hi everyone. I was beginning to wonder why nobody said anything about the code, so I went back to the code to take a look. I'm glad I said it was a long time ago, because the code contained a lot of crap it didn't need in it. I was so worried about what to do if the logfile was gone, I didn't think about the code I was actually writing. So, I apologize for the code, and I will give you the updated version that does everything that the first code does.

And it's only 3 lines.

Code: Visual Basic [Select]
  1. Public Sub LogToFile(LogMessage As String)
  2.         Using sw = AppendText(logfilename)
  3.             sw.WriteLine(LogMessage)
  4.         End Using
  5.     End Sub

I forgot that append would add to the file or create the file.
So yep, I'm dumb.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #15 on: June 01, 2016, 01:05 AM »
Hm, being a mostly Java developer, with just some occasional .NET (C#) work, I didn't take a very close look, as I'm used to Log4J as a logging framework, so I intend to use Log4Net in my .NET projects. Though I've written similar loggers in older projects :tellme:

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #16 on: June 02, 2016, 03:56 AM »
Hm, being a mostly Java developer, with just some occasional .NET (C#) work, I didn't take a very close look, as I'm used to Log4J as a logging framework, so I intend to use Log4Net in my .NET projects. Though I've written similar loggers in older projects :tellme:

Hi, and thanks for posting. I always respect Java developers. I tried learning it once and just didn't do very good. I've heard of Log4 before. Is it free? I'm sure I can find out by searching though. Have a great day.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #17 on: June 02, 2016, 05:01 AM »
I tried learning it once and just didn't do very good.
Especially the GUI-part, when not building a web application, can be hard. I try to avoid Java-based-GUI applications where possible :tellme:

I've heard of Log4 before. Is it free?
Like a lot of Java libraries, it's free and open source, Log4J just like the .NET port Log4Net.

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #18 on: June 02, 2016, 05:12 AM »
Especially the GUI-part, when not building a web application, can be hard. I try to avoid Java-based-GUI applications where possible :tellme:

Like a lot of Java libraries, it's free and open source, Log4J just like the .NET port Log4Net.

Yep, the GUI part is what I was trying to learn. I didn't do very good and found that understanding it at all had a steep learning curve.

Also, I tried Log4Net and couldn't get it to work. Right now, I'm making a Log class now and besides writing to a file, it will also write to the event log just as soon as I know how to do it :)




f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #19 on: June 02, 2016, 10:11 AM »
Yep, the GUI part is what I was trying to learn. I didn't do very good and found that understanding it at all had a steep learning curve.
Frankly, writing GUIs in Java sucks - it's become a lot better with JavaFX, but all the old toolkits for it are bloody annoying to use, and until Java 8 (released in 2014) with lambda support, it was generally gruesome.

If you want to do desktop windows applications, it doesn't get much easier than .NET - especially with the Visual Studio tooling.

Java isn't a complicated language, though, and it's fine for a lot of stuff :)
- carpe noctem

jumper

  • Participant
  • Joined in 2016
  • *
  • default avatar
  • Posts: 21
    • View Profile
    • Donate to Member
Re: Hi, first post in the developers section
« Reply #20 on: June 02, 2016, 10:18 AM »
Frankly, writing GUIs in Java sucks

Yea, it was a very long time ago when I tried to write a GUI. At that time I think the only thing that was available was the SWING libraries.