Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Adding text to .txt file in webserver

Hello,

I made a logs.txt to see how many times user connects to my application, i saved it in .txt file (On my pc)

But i want to do the same thing to a file (log.txt) on webserver

Code
/*
            FileStream stream = null;
            string fileName = @"E:/logs.txt";
            stream = new FileStream(fileName, FileMode.OpenOrCreate);
            using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) ;
            try
            {
                using (var writer = File.AppendText(fileName))
                {
                    writer.WriteLine(welcome.Text);
                }
            }
            catch (Exception exp)
            {
                Console.Write(exp.Message);
            }
            */
What kind of server side software are you running?

FTP? Php?
[Image: Syr3DlV.png]
As per another post

I would strongly advise log4net for this. What you are doing here isn't thread safe and could easily result in exceptions.


Code
class Program
   {
       private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

       static void Main(string[] args)
       {
           log.Info("Hello logging world!");
           Console.WriteLine("Hit enter");
           Console.ReadLine();
       }
   }

Have a look here > https://stackify.com/log4net-guide-dotnet-logging/
(Apr 23, 2020 08:12 AM)gigajew Wrote:  What kind of server side software are you running?

FTP? Php?

FTP brother.
(Apr 23, 2020 09:29 AM)Formula 1 Wrote:  FTP brother.

Here you go https://docs.microsoft.com/en-us/dotnet/...s-with-ftp
[Image: Syr3DlV.png]
(Apr 23, 2020 07:15 AM)Formula 1 Wrote:  Hello,

I made a logs.txt to see how many times user connects to my application, i saved it in .txt file (On my pc)

But i want to do the same thing to a file (log.txt) on webserver

Code
/*
           FileStream stream = null;
           string fileName = @"E:/logs.txt";
           stream = new FileStream(fileName, FileMode.OpenOrCreate);
           using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8)) ;
           try
           {
               using (var writer = File.AppendText(fileName))
               {
                   writer.WriteLine(welcome.Text);
               }
           }
           catch (Exception exp)
           {
               Console.Write(exp.Message);
           }
           */

pm me: hackerman#0001
you're going to want to download the existing logs.txt from your webserver, append the new line and re-upload it making sure to overwrite the existing file.

If you don't want to hardcode your FTP creds into your application you should look into something like this
https://www.php.net/manual/en/function.f...ntents.php

Where your application will make a single web request and the server will handle logging and writing to the file.
This is safer and will also help if your application has lots of users.

lmk if you need any help with this
(Apr 23, 2020 08:01 PM)ОЯВІТ Wrote:  you're going to want to download the existing logs.txt from your webserver, append the new line and re-upload it making sure to overwrite the existing file.

If you don't want to hardcode your FTP creds into your application you should look into something like this
https://www.php.net/manual/en/function.f...ntents.php

Where your application will make a single web request and the server will handle logging and writing to the file.
This is safer and will also help if your application has lots of users.

lmk if you need any help with this

I wouldn't take the first approach, as this file grows it will become more and more bandwidth intensive. If you get to the point where that file is 2MB, you are downloading it, appending on the client, then uploading the 2MB again. And this will just get worse, plus the app has the details to connect which is already a code smell.

The second option is much better, but it's still important to make sure that when the server writes to disk its doing so in a thread safe manner.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by MarwenSep 24, 2022 04:19 PM 10 638 Sep 24, 2022 04:19 PM
Last Post: BinaryFox
[Shell] by Cyber "Max" ShamanJan 25, 2022 08:56 PM 3 184 Jan 25, 2022 08:56 PM
Last Post: Cyber "Max" Shaman
[Python] by 0u1lawJul 15, 2021 08:20 AM 2 188 Jul 15, 2021 08:20 AM
Last Post: 3phase