Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
A
aspnetcore
管理
动态
成员
标记
计划
议题
议题看板
里程碑
Wiki
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
构建
流水线
作业
流水线计划
产物
部署
发布
Package registry
Container registry
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
模型实验
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
dotnet
aspnetcore
提交
02812539
未验证
提交
02812539
编辑于
3 years ago
作者:
William Godbe
提交者:
GitHub
3 years ago
浏览文件
操作
下载
补丁
差异文件
Fix FileLoggerProcessorTests (#35305)
* Fix FileLoggerProcessorTests * Fixup * Whoops * Fixup * Feedback
上级
4d576fb6
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
1
隐藏空白变更内容
行内
左右并排
显示
1 个更改的文件
src/Middleware/HttpLogging/test/FileLoggerProcessorTests.cs
+51
-31
51 个添加, 31 个删除
src/Middleware/HttpLogging/test/FileLoggerProcessorTests.cs
有
51 个添加
和
31 个删除
src/Middleware/HttpLogging/test/FileLoggerProcessorTests.cs
+
51
−
31
浏览文件 @
02812539
...
...
@@ -19,6 +19,11 @@ namespace Microsoft.AspNetCore.HttpLogging
{
public
class
FileLoggerProcessorTests
{
private
string
_messageOne
=
"Message one"
;
private
string
_messageTwo
=
"Message two"
;
private
string
_messageThree
=
"Message three"
;
public
FileLoggerProcessorTests
()
{
TempPath
=
Path
.
GetTempFileName
()
+
"_"
;
...
...
@@ -41,12 +46,12 @@ namespace Microsoft.AspNetCore.HttpLogging
};
await
using
(
var
logger
=
new
FileLoggerProcessor
(
new
OptionsWrapperMonitor
<
W3CLoggerOptions
>(
options
),
new
HostingEnvironment
(),
NullLoggerFactory
.
Instance
))
{
logger
.
EnqueueMessage
(
"M
essage
o
ne
"
);
logger
.
EnqueueMessage
(
_m
essage
O
ne
);
fileName
=
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0000.txt"
));
// Pause for a bit before disposing so logger can finish logging
try
{
await
WaitForFile
(
fileName
).
DefaultTimeout
();
await
WaitForFile
(
fileName
,
_messageOne
.
Length
).
DefaultTimeout
();
}
catch
{
...
...
@@ -60,7 +65,7 @@ namespace Microsoft.AspNetCore.HttpLogging
}
Assert
.
True
(
File
.
Exists
(
fileName
));
Assert
.
Equal
(
"M
essage
o
ne
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName
));
Assert
.
Equal
(
_m
essage
O
ne
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName
));
}
finally
{
...
...
@@ -86,14 +91,14 @@ namespace Microsoft.AspNetCore.HttpLogging
};
await
using
(
var
logger
=
new
FileLoggerProcessor
(
new
OptionsWrapperMonitor
<
W3CLoggerOptions
>(
options
),
new
HostingEnvironment
(),
NullLoggerFactory
.
Instance
))
{
logger
.
EnqueueMessage
(
"M
essage
o
ne
"
);
logger
.
EnqueueMessage
(
"M
essage
t
wo
"
);
logger
.
EnqueueMessage
(
_m
essage
O
ne
);
logger
.
EnqueueMessage
(
_m
essage
T
wo
);
fileName1
=
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0000.txt"
));
fileName2
=
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0001.txt"
));
// Pause for a bit before disposing so logger can finish logging
try
{
await
WaitForFile
(
fileName2
).
DefaultTimeout
();
await
WaitForFile
(
fileName2
,
_messageTwo
.
Length
).
DefaultTimeout
();
}
catch
{
...
...
@@ -114,8 +119,8 @@ namespace Microsoft.AspNetCore.HttpLogging
Assert
.
True
(
File
.
Exists
(
fileName1
));
Assert
.
True
(
File
.
Exists
(
fileName2
));
Assert
.
Equal
(
"M
essage
o
ne
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName1
));
Assert
.
Equal
(
"M
essage
t
wo
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName2
));
Assert
.
Equal
(
_m
essage
O
ne
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName1
));
Assert
.
Equal
(
_m
essage
T
wo
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName2
));
}
finally
{
...
...
@@ -145,13 +150,13 @@ namespace Microsoft.AspNetCore.HttpLogging
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
logger
.
EnqueueMessage
(
"M
essage
"
);
logger
.
EnqueueMessage
(
_m
essage
One
);
}
lastFileName
=
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0009.txt"
));
// Pause for a bit before disposing so logger can finish logging
try
{
await
WaitForFile
(
lastFileName
).
DefaultTimeout
();
await
WaitForFile
(
lastFileName
,
_messageOne
.
Length
).
DefaultTimeout
();
for
(
int
i
=
0
;
i
<
6
;
i
++)
{
await
WaitForRoll
(
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.
{
i
:
0000
}
.txt"
))).
DefaultTimeout
();
...
...
@@ -211,11 +216,11 @@ namespace Microsoft.AspNetCore.HttpLogging
{
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
logger
.
EnqueueMessage
(
"M
essage
"
);
logger
.
EnqueueMessage
(
_m
essage
One
);
}
var
filePath
=
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0002.txt"
));
// Pause for a bit before disposing so logger can finish logging
await
WaitForFile
(
filePath
).
DefaultTimeout
();
await
WaitForFile
(
filePath
,
_messageOne
.
Length
).
DefaultTimeout
();
}
// Second instance should pick up where first one left off
...
...
@@ -223,11 +228,11 @@ namespace Microsoft.AspNetCore.HttpLogging
{
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
logger
.
EnqueueMessage
(
"M
essage
"
);
logger
.
EnqueueMessage
(
_m
essage
One
);
}
var
filePath
=
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0005.txt"
));
// Pause for a bit before disposing so logger can finish logging
await
WaitForFile
(
filePath
).
DefaultTimeout
();
await
WaitForFile
(
filePath
,
_messageOne
.
Length
).
DefaultTimeout
();
}
var
actualFiles1
=
new
DirectoryInfo
(
path
)
...
...
@@ -246,9 +251,9 @@ namespace Microsoft.AspNetCore.HttpLogging
options
.
RetainedFileCountLimit
=
5
;
await
using
(
var
logger
=
new
FileLoggerProcessor
(
new
OptionsWrapperMonitor
<
W3CLoggerOptions
>(
options
),
new
HostingEnvironment
(),
NullLoggerFactory
.
Instance
))
{
logger
.
EnqueueMessage
(
"M
essage
"
);
logger
.
EnqueueMessage
(
_m
essage
One
);
// Pause for a bit before disposing so logger can finish logging
await
WaitForFile
(
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0006.txt"
))).
DefaultTimeout
();
await
WaitForFile
(
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0006.txt"
))
,
_messageOne
.
Length
).
DefaultTimeout
();
await
WaitForRoll
(
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0000.txt"
))).
DefaultTimeout
();
await
WaitForRoll
(
Path
.
Combine
(
path
,
FormattableString
.
Invariant
(
$"
{
options
.
FileName
}{
now
.
Year
:
0000
}{
now
.
Month
:
00
}{
now
.
Day
:
00
}
.0001.txt"
))).
DefaultTimeout
();
}
...
...
@@ -298,10 +303,10 @@ namespace Microsoft.AspNetCore.HttpLogging
await
using
(
var
logger
=
new
FileLoggerProcessor
(
new
OptionsWrapperMonitor
<
W3CLoggerOptions
>(
options
),
new
HostingEnvironment
(),
NullLoggerFactory
.
Instance
))
{
logger
.
EnqueueMessage
(
"M
essage
o
ne
"
);
logger
.
EnqueueMessage
(
"M
essage
t
wo
"
);
logger
.
EnqueueMessage
(
_m
essage
O
ne
);
logger
.
EnqueueMessage
(
_m
essage
T
wo
);
// Pause for a bit before disposing so logger can finish logging
await
WaitForFile
(
fileName2
).
DefaultTimeout
();
await
WaitForFile
(
fileName2
,
_messageTwo
.
Length
).
DefaultTimeout
();
}
// Even with a big enough FileSizeLimit, we still won't try to write to files from a previous instance.
...
...
@@ -309,9 +314,9 @@ namespace Microsoft.AspNetCore.HttpLogging
await
using
(
var
logger
=
new
FileLoggerProcessor
(
new
OptionsWrapperMonitor
<
W3CLoggerOptions
>(
options
),
new
HostingEnvironment
(),
NullLoggerFactory
.
Instance
))
{
logger
.
EnqueueMessage
(
"M
essage
t
hree
"
);
logger
.
EnqueueMessage
(
_m
essage
T
hree
);
// Pause for a bit before disposing so logger can finish logging
await
WaitForFile
(
fileName3
).
DefaultTimeout
();
await
WaitForFile
(
fileName3
,
_messageThree
.
Length
).
DefaultTimeout
();
}
var
actualFiles
=
new
DirectoryInfo
(
path
)
...
...
@@ -326,9 +331,9 @@ namespace Microsoft.AspNetCore.HttpLogging
Assert
.
True
(
File
.
Exists
(
fileName2
));
Assert
.
True
(
File
.
Exists
(
fileName3
));
Assert
.
Equal
(
"M
essage
o
ne
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName1
));
Assert
.
Equal
(
"M
essage
t
wo
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName2
));
Assert
.
Equal
(
"M
essage
t
hree
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName3
));
Assert
.
Equal
(
_m
essage
O
ne
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName1
));
Assert
.
Equal
(
_m
essage
T
wo
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName2
));
Assert
.
Equal
(
_m
essage
T
hree
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName3
));
}
finally
{
...
...
@@ -364,13 +369,13 @@ namespace Microsoft.AspNetCore.HttpLogging
await
using
(
var
logger
=
new
FileLoggerProcessor
(
monitor
,
new
HostingEnvironment
(),
NullLoggerFactory
.
Instance
))
{
logger
.
EnqueueMessage
(
"M
essage
o
ne
"
);
await
WaitForFile
(
fileName1
).
DefaultTimeout
();
logger
.
EnqueueMessage
(
_m
essage
O
ne
);
await
WaitForFile
(
fileName1
,
_messageOne
.
Length
).
DefaultTimeout
();
options
.
LoggingFields
=
W3CLoggingFields
.
Date
;
monitor
.
InvokeChanged
();
logger
.
EnqueueMessage
(
"M
essage
t
wo
"
);
logger
.
EnqueueMessage
(
_m
essage
T
wo
);
// Pause for a bit before disposing so logger can finish logging
await
WaitForFile
(
fileName2
).
DefaultTimeout
();
await
WaitForFile
(
fileName2
,
_messageTwo
.
Length
).
DefaultTimeout
();
}
var
actualFiles
=
new
DirectoryInfo
(
path
)
...
...
@@ -384,8 +389,8 @@ namespace Microsoft.AspNetCore.HttpLogging
Assert
.
True
(
File
.
Exists
(
fileName1
));
Assert
.
True
(
File
.
Exists
(
fileName2
));
Assert
.
Equal
(
"M
essage
o
ne
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName1
));
Assert
.
Equal
(
"M
essage
t
wo
"
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName2
));
Assert
.
Equal
(
_m
essage
O
ne
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName1
));
Assert
.
Equal
(
_m
essage
T
wo
+
Environment
.
NewLine
,
File
.
ReadAllText
(
fileName2
));
}
finally
{
...
...
@@ -393,12 +398,27 @@ namespace Microsoft.AspNetCore.HttpLogging
}
}
private
async
Task
WaitForFile
(
string
fileName
)
private
async
Task
WaitForFile
(
string
fileName
,
int
length
)
{
while
(!
File
.
Exists
(
fileName
))
{
await
Task
.
Delay
(
100
);
}
while
(
true
)
{
try
{
if
(
File
.
ReadAllText
(
fileName
).
Length
>=
length
)
{
break
;
}
}
catch
{
// Continue
}
await
Task
.
Delay
(
10
);
}
}
private
async
Task
WaitForRoll
(
string
fileName
)
...
...
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录