3

I am new in Linux and my API was created in .net core and running in Docker. The system i create will write/create a txt file that will input all errors logged in the API. My code to write is this

`public class WriteLogs {

    public void ErrorLogFile(string traceNo, string errorMsg)
    {
        DirectoryInfo dir = new DirectoryInfo(Startup.errorPath);
        if (!dir.Exists)
        {
            dir.Create();
        }

        using (StreamWriter swLog = File.AppendText(Startup.errorPath + Startup.errorFileName + DateTime.Now.ToString("MMddyyyy") + ".txt"))
        {
            swLog.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff") + " - Trace Number : " + traceNo + "   " + errorMsg + "\n");
        }
    }
}`

the value in my startup is located in my appSettings.json file :

"ErrorPath": "C:\\BP\\", "ErrorFileName": "BP-ParamLogs_",

This is working in windows environment but when i transfer my program to linux and change the ErrorPath to:

"ErrorPath": "/home/Logs/",

the file was not created.

My question is, do my syntax works in linux to write txt file or my path was wrong?

Enjo A
  • 57
  • 2
  • 6
  • Turn your slashes (`"ErrorPath": "/home/Logs/"`) and also check if you have a write access to `/home/Logs/` – vasily.sib May 14 '20 at 04:50
  • what is the error message? maybe it is helpful to wrap your code in a `try-catch`-block to get the error message. I am assuming that you have to reference the path like `/home/Logs/` – Daniel May 14 '20 at 04:50
  • Hi Daniel, theres no error being displayed, its just it didnt create the logs in the server – Enjo A May 14 '20 at 04:55
  • tried writing the path to `/home/Logs/` but it didnt work – Enjo A May 14 '20 at 05:28

2 Answers2

2

To answer your specific question about the syntax. \home\Logs\ should be /home/Logs/ as Linux uses forward-slash for path separator.

There's a chance that your program does not have write access to the /home/ directory. I've personally tried a similar program and I ran into System.UnauthorizedAccessException during the logs directory creation step.

Try running your program from the terminal with dotnet run to see the exception you might be getting. If you are also running into the System.UnauthorizedAccessException then run as root with sudo dotnet run

hforeste
  • 21
  • 3
0

i just solved the problem by using command:

find . -name BP-Param*

from the first directory. After executing it. it was in the docker directory.

Enjo A
  • 57
  • 2
  • 6