Docker容器中的代码工作方式不同

izkcnapc  于 2023-01-29  发布在  Docker
关注(0)|答案(1)|浏览(137)

我已经用mcr.microsoft.com/dotnet/sdk:6.0将consoleApp移植到Docker容器。现在我注意到容器内的代码行为不同。在容器外,当连接丢失时会生成异常,但在容器内不会!原因可能是什么?
非常感谢你的帮助。

public static async Task FtpUploadAsync(string hostname, string username, string password, string[] uploadfiles)
    {
        try
        {
            var token = new CancellationToken();
            using (AsyncFtpClient client = new AsyncFtpClient())
            {
                client.Host = hostname;
                client.Port = 21;
                client.Credentials.UserName = username;
                client.Credentials.Password = password;
                client.Config.EncryptionMode = FtpEncryptionMode.None;
                client.Config.InternetProtocolVersions = FtpIpVersion.IPv4;
                client.Config.ValidateAnyCertificate = true;
                client.Config.ConnectTimeout = 15000;
                await client.AutoConnect(token);

                Console.WriteLine("Connectetd!");

                foreach (var varFilePath in uploadfiles)
                {
                    Console.WriteLine("Uploading File: " + varFilePath.GetFtpFileName());
                    await client.UploadFile(varFilePath, "/" + varFilePath.GetFtpFileName(), FtpRemoteExists.Overwrite, true, token: token);
                    File.Delete(varFilePath);
                    WriteLogFile.WriteLog("File Uploaded", varFilePath.GetFtpFileName());
                }
            }
        }
        catch (Exception ex)
        {
            WriteLogFile.WriteLog("catch", "Ftp-Uploader " + ex);
            Console.WriteLine("Ftp-Uploader " + ex);
        }
    }

你好,我如何包括代码来写调试日志!很遗憾我不能得到它!对不起....

using FluentFTP;          // from NuGet package FluentFTP
using FluentFTP.Logging;  // from NuGet package FluentFTP.Logging

var builder = WebApplication.CreateBuilder(args);

var logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File("logs/Net6Tester.txt", rollingInterval: 
RollingInterval.Day)
.CreateLogger();

client.Logger = new FtpLogAdapter(logger);

这是日志!当我在windows系统上运行coed时,它会在断开连接后处理。如果我在容器中运行它,它会立即想要继续处理数组中的下一个文件!!

Log -- Windows System
######################
# AutoConnect()

# AutoDetect(True, False)

# Connect(False)
Status:   FluentFTP 44.0.1.0
Status:   Connecting to IP #1= ***:21
Status:   Waiting for a response
Response: 220 Xlight FTP Server 3.9 ready... [738548,429d]
Status:   Detected FTP server: XLight
Command:  AUTH TLS
Status:   Waiting for response to: AUTH TLS
Response: 431 No security resource for TLS/SSL encryption, 
probably there is no selected SSL certificate [370ms]
Command:  USER ***
Status:   Waiting for response to: USER ***
Response: 331 Password required for *** [394ms]
Command:  PASS ***
Status:   Waiting for response to: PASS ***
Response: 230 Login OK [297ms]
Command:  FEAT
Status:   Waiting for response to: FEAT
Response: 211-Features supported
Response: REST STREAM
Response: EPRT
Response: EPSV
Response: SIZE
Response: MDTM
Response: MFMT
Response: AUTH
Response: PBSZ
Response: PROT
Response: MLST type*;size*;modify*;
Response: MLSD
Response: 211 End [72ms]
Status:   Text encoding: 
System.Text.UTF8Encoding+UTF8EncodingSealed
Command:  OPTS UTF8 ON
Status:   Waiting for response to: OPTS UTF8 ON
Response: 502 This function is disabled [35ms]
Command:  SYST
Status:   Waiting for response to: SYST
Response: 215 UNIX Type: L8 [18ms]
Status:   Listing parser set to: Machine
Command:  PWD
Status:   Waiting for response to: PWD
Response: 257 "/" [6ms]
Connectetd!
Uploading File: test-big.opus
[11:18:28 INF] Uploading file test-big.opus

# UploadFile("C:\temp\ftpUpload\test-big.opus", "/test-big.opus", 
Overwrite, True, None)

# FileExists("/test-big.opus")
Command:  SIZE /test-big.opus
Status:   Waiting for response to: SIZE /test-big.opus
Response: 550 Can't find file "test-big.opus". [70ms]

# DirectoryExists("/")

# OpenWrite("/test-big.opus", Binary)
Command:  TYPE I
Status:   Waiting for response to: TYPE I
Response: 200 Type set to I. [23ms]

# OpenDataStreamAsync("STOR /test-big.opus", 0)

# OpenPassiveDataStreamAsync(PASV, "STOR /test-big.opus", 0)
Command:  PASV
Status:   Waiting for response to: PASV
Response: 227 Entering Passive Mode (192,168,178,30,195,196) 
[14ms]
Status:   Connecting to IP #1= ***:50116
Command:  STOR /test-big.opus
Status:   Waiting for response to: STOR /test-big.opus
Response: 150 Opening BINARY mode data connection for test- 
big.opus. [8ms]
Status:   Closing/Disposing FtpSocketStream(data connection)
Status:   Attempting upload resume at position 1048576

# OpenAppend("/test-big.opus", Binary)

# GetFileSize("/test-big.opus", -1)
Command:  SIZE /test-big.opus
Status:   Waiting for response to: SIZE /test-big.opus
Status:   Closing/Disposing FtpSocketStream(data connection)

# Dispose()
Status:   Disposing FtpClient object...
Status:   Closing/Disposing FtpSocketStream(control connection)
Status:   Closing/Disposing FtpSocketStream(control connection)

###############################################################
Log-File Docker Container
#########################

# AutoConnect()
# AutoDetect(True, False)
# Connect(False)
Status:   FluentFTP 44.0.1.0
Status:   Connecting to IP #1= ***:21
Status:   Waiting for a response
Response: 220 Xlight FTP Server 3.9 ready... [738548.43d]
Status:   Detected FTP server: XLight
Command:  AUTH TLS
Status:   Waiting for response to: AUTH TLS
Response: 431 No security resource for TLS/SSL encryption, 
probably there is no selected SSL certificate [390ms]
Command:  USER ***
Status:   Waiting for response to: USER ***
Response: 331 Password required for *** [398ms]
Command:  PASS ***
Status:   Waiting for response to: PASS ***
Response: 230 Login OK [299ms]
Command:  FEAT
Response: 211-Features supported
Response: REST STREAM
Response: EPRT
Response: EPSV
Response: SIZE
Response: MDTM
Response: MFMT
Response: AUTH
Response: PBSZ
Response: PROT
Response: MLST type*;size*;modify*;
Response: MLSD
Response: 211 End [73ms]
Status:   Text encoding: 
System.Text.UTF8Encoding+UTF8EncodingSealed
Command:  OPTS UTF8 ON
Status:   Waiting for response to: OPTS UTF8 ON
Response: 502 This function is disabled [16ms]
Command:  SYST
Status:   Waiting for response to: SYST
Response: 215 UNIX Type: L8 [17ms]
Status:   Listing parser set to: Machine
Command:  PWD
Status:   Waiting for response to: PWD
Response: 257 "/" [6ms]
Connectetd!
Uploading File: test-big.opus
[10:18:36 INF] Uploading file test-big.opus

# UploadFile("/var/lib/ftp-uploader/input/test-big.opus", 
"/test- 
big.opus", Overwrite, True, None)
# FileExists("/test-big.opus")
Command:  SIZE /test-big.opus
Status:   Waiting for response to: SIZE /test-big.opus
Response: 213 926600 [58ms]

# DeleteFile("/test-big.opus")
Command:  DELE /test-big.opus
Status:   Waiting for response to: DELE /test-big.opus
Response: 250 Deleted file "test-big.opus". [35ms]

# OpenWrite("/test-big.opus", Binary)
Command:  TYPE I
Status:   Waiting for response to: TYPE I
Response: 200 Type set to I. [14ms]

# OpenDataStreamAsync("STOR /test-big.opus", 0)

# OpenPassiveDataStreamAsync(PASV, "STOR /test-big.opus", 0)
Command:  PASV
Status:   Waiting for response to: PASV
Response: 227 Entering Passive Mode (192,168,178,30,195,206) 
[11ms]
Status:   Connecting to IP #1= ***:50126
Command:  STOR /test-big.opus
Status:   Waiting for response to: STOR /test-big.opus
Response: 150 Opening BINARY mode data connection for test- 
big.opus. [1ms]
Status:   Closing/Disposing FtpSocketStream(data connection)
Status:   IOException for file /var/lib/ftp- 
uploader/input/test-big.opus : Unable to write data to the 
transport connection: Broken pipe.
Status:   Failed to upload file.
Uploading File: test-small.opus
[10:18:45 INF] Uploading file test-small.opus

# UploadFile("/var/lib/ftp-uploader/input/test-small.opus", 
"/test-small.opus", Overwrite, True, None)
# FileExists("/test-small.opus")

Command:  SIZE /test-small.opus
Status:   Waiting for response to: SIZE /test-small.opus
aemubtdh

aemubtdh1#

要从FTP客户端配置日志记录,可以使用以下命令:
根据FluentFTP日志记录页面,您可以通过Serilog设置日志记录。添加以下NuGet包:

  • 塞里洛格
  • Serilog.Extensions.Logging
  • Serilog.Sinks.File
  • (也可选择登录到控制台)Serilog.Sinks.Console

在程序开始时配置日志记录:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File("logs/Net6Tester.txt", rollingInterval: RollingInterval.Day)
    .WriteTo.Console() // comment out to not log to console
    .CreateLogger();

然后在创建AsyncFtpClient时,将其配置为记录到Serilog:

using (AsyncFtpClient client = new AsyncFtpClient())
{
    var logger = LoggerFactory.Create(builder => builder.AddSerilog())
        .CreateLogger<UploadClient>();
    client.Logger = new FtpLogAdapter(logger);

您也可以使用以下内容登录自己:

Log.Logger.Information("Uploading file {0}", varFilePath.GetFtpFileName());

请首先确保它在您的主机上按预期记录。然后尝试从Docker容器获取日志。

相关问题