Révision | 86e26ecde9d36b0074e6fb8a2a2b26250bb81d51 (tree) |
---|---|
l'heure | 2022-11-30 01:06:13 |
Auteur | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[MOD] UserDialogProxy の名前空間を UI 配下から UI.Dialog 配下に移動
[FIX] JsonHelper.ToJsonString のメソッド名が破損していた不具合を修正
[MOD] キャプション書式化機能でキャプションの取得と書式化を行う機能の区別を明確にした
[MOD] MessageRepository の基底処理を CleanAuLait 側と同様に AbstractMessageRepository クラスに分割
[ADD] CleanAuLait 側で追加された機能の取り込み(PathHelper, DateTimeHelper, ログ系)
@@ -0,0 +1,19 @@ | ||
1 | +using System; | |
2 | +using System.Windows; | |
3 | + | |
4 | +namespace CleanAuLait48.Adaptor.Boundary.Gateway.UI.Dialog | |
5 | +{ | |
6 | + public interface IUserDialogProxy | |
7 | + { | |
8 | + void ShowInfo(string msg); | |
9 | + void ShowInfo(string msg, string captionKey); | |
10 | + void ShowWarn(string msg); | |
11 | + void ShowWarn(string msg, string captionKey); | |
12 | + void ShowError(Exception e); | |
13 | + void ShowError(Exception e, string captionKey); | |
14 | + void ShowError(string msg); | |
15 | + void ShowError(string msg, string captionKey); | |
16 | + MessageBoxResult ShowConfirm(string msg); | |
17 | + MessageBoxResult ShowConfirm(string msg, string captionKey); | |
18 | + } | |
19 | +} | |
\ No newline at end of file |
@@ -1,17 +0,0 @@ | ||
1 | -using CleanAuLait48.Adaptor.Gateway.UI; | |
2 | -using System; | |
3 | - | |
4 | -namespace CleanAuLait48.Adaptor.Boundary.Gateway.UI | |
5 | -{ | |
6 | - public interface IUserDialogProxy | |
7 | - { | |
8 | - void ShowInfo(string msg); | |
9 | - void ShowInfo(string msg, DlgInfo uiInfo); | |
10 | - void ShowWarn(string msg); | |
11 | - void ShowWarn(string msg, DlgInfo uiInfo); | |
12 | - void ShowError(Exception e); | |
13 | - void ShowError(Exception e, DlgInfo uiInfo); | |
14 | - void ShowError(string msg); | |
15 | - void ShowError(string msg, DlgInfo uiInfo); | |
16 | - } | |
17 | -} | |
\ No newline at end of file |
@@ -1,4 +1,4 @@ | ||
1 | -using CleanAuLait48.Adaptor.Boundary.Gateway.UI; | |
1 | +using CleanAuLait48.Adaptor.Boundary.Gateway.UI.Dialog; | |
2 | 2 | using CleanAuLait48.UseCase.Request; |
3 | 3 | using CleanAuLait48.UseCase.Response; |
4 | 4 |
@@ -1,6 +1,6 @@ | ||
1 | 1 | using CleanAuLait48.Adaptor.Boundary.Controller; |
2 | 2 | using CleanAuLait48.Adaptor.Boundary.Controller.Handler; |
3 | -using CleanAuLait48.Adaptor.Boundary.Gateway.UI; | |
3 | +using CleanAuLait48.Adaptor.Boundary.Gateway.UI.Dialog; | |
4 | 4 | using CleanAuLait48.UseCase.Request; |
5 | 5 | using CleanAuLait48.UseCase.Response; |
6 | 6 | using NLog; |
@@ -1,6 +1,6 @@ | ||
1 | 1 | using CleanAuLait48.Adaptor.Boundary.Controller; |
2 | 2 | using CleanAuLait48.Adaptor.Boundary.Controller.Handler; |
3 | -using CleanAuLait48.Adaptor.Boundary.Gateway.UI; | |
3 | +using CleanAuLait48.Adaptor.Boundary.Gateway.UI.Dialog; | |
4 | 4 | using CleanAuLait48.UseCase.Request; |
5 | 5 | using CleanAuLait48.UseCase.Response; |
6 | 6 | using NLog; |
@@ -0,0 +1,92 @@ | ||
1 | +using CleanAuLait48.Adaptor.Boundary.Gateway.UI.Dialog; | |
2 | +using CleanAuLait48.Core.Resource; | |
3 | +using System; | |
4 | +using System.Windows; | |
5 | + | |
6 | +namespace CleanAuLait48.Adaptor.Gateway.UI.Dialog | |
7 | +{ | |
8 | + public class UserDialogProxy : IUserDialogProxy | |
9 | + { | |
10 | + private readonly ICaptionFormatter captionFormatter; | |
11 | + | |
12 | + public UserDialogProxy(ICaptionFormatter captionFormatter) | |
13 | + { | |
14 | + this.captionFormatter = captionFormatter; | |
15 | + } | |
16 | + | |
17 | + public void ShowInfo(string msg) | |
18 | + { | |
19 | + ShowInfo(msg, null); | |
20 | + } | |
21 | + | |
22 | + public void ShowInfo(string msg, string captionKey) | |
23 | + { | |
24 | + string caption = this.captionFormatter.FormatInfoCaption(captionKey); | |
25 | + MessageBoxImage icon = MessageBoxImage.Information; | |
26 | + | |
27 | + ShowNotify(caption, msg, icon); | |
28 | + } | |
29 | + | |
30 | + public void ShowWarn(string msg) | |
31 | + { | |
32 | + ShowWarn(msg, null); | |
33 | + } | |
34 | + | |
35 | + public void ShowWarn(string msg, string captionKey) | |
36 | + { | |
37 | + string caption = this.captionFormatter.FormatWarnCaption(captionKey); | |
38 | + MessageBoxImage icon = MessageBoxImage.Warning; | |
39 | + | |
40 | + ShowNotify(caption, msg, icon); | |
41 | + } | |
42 | + | |
43 | + public void ShowError(Exception e) | |
44 | + { | |
45 | + ShowError(e, null); | |
46 | + } | |
47 | + | |
48 | + public void ShowError(Exception e, string captionKey) | |
49 | + { | |
50 | + string caption = this.captionFormatter.FormatExceptionCaption(e, captionKey); | |
51 | + MessageBoxImage icon = MessageBoxImage.Error; | |
52 | + | |
53 | + ShowNotify(caption, e.Message, icon); | |
54 | + } | |
55 | + | |
56 | + public void ShowError(string msg) | |
57 | + { | |
58 | + ShowError(msg, null); | |
59 | + } | |
60 | + | |
61 | + public void ShowError(string msg, string captionKey) | |
62 | + { | |
63 | + string caption = this.captionFormatter.FormatErrorCaption(captionKey); | |
64 | + MessageBoxImage icon = MessageBoxImage.Error; | |
65 | + | |
66 | + ShowNotify(caption, msg, icon); | |
67 | + } | |
68 | + | |
69 | + public static void ShowNotify(string caption, string msg, MessageBoxImage icon) | |
70 | + { | |
71 | + MessageBox.Show(msg, caption, MessageBoxButton.OK, icon); | |
72 | + } | |
73 | + | |
74 | + public MessageBoxResult ShowConfirm(string msg) | |
75 | + { | |
76 | + return ShowConfirm(msg, null); | |
77 | + } | |
78 | + | |
79 | + public MessageBoxResult ShowConfirm(string msg, string captionKey) | |
80 | + { | |
81 | + string caption = this.captionFormatter.FormatConfirmCaption(captionKey); | |
82 | + MessageBoxImage icon = MessageBoxImage.Question; | |
83 | + | |
84 | + return ShowConfirm(caption, msg, icon); | |
85 | + } | |
86 | + | |
87 | + public static MessageBoxResult ShowConfirm(string caption, string msg, MessageBoxImage icon) | |
88 | + { | |
89 | + return MessageBox.Show(msg, caption, MessageBoxButton.YesNo, icon); | |
90 | + } | |
91 | + } | |
92 | +} |
@@ -1,16 +0,0 @@ | ||
1 | -namespace CleanAuLait48.Adaptor.Gateway.UI | |
2 | -{ | |
3 | - public class DlgInfo | |
4 | - { | |
5 | - public string Caption { get; protected set; } = string.Empty; | |
6 | - | |
7 | - protected DlgInfo() | |
8 | - { | |
9 | - } | |
10 | - | |
11 | - public DlgInfo(string caption) | |
12 | - { | |
13 | - Caption = caption; | |
14 | - } | |
15 | - } | |
16 | -} |
@@ -1,74 +0,0 @@ | ||
1 | -using CleanAuLait48.Adaptor.Boundary.Gateway.UI; | |
2 | -using CleanAuLait48.Core.Resource; | |
3 | -using System; | |
4 | -using System.Windows.Forms; | |
5 | - | |
6 | -namespace CleanAuLait48.Adaptor.Gateway.UI | |
7 | -{ | |
8 | - public class UserDialogProxy : IUserDialogProxy | |
9 | - { | |
10 | - private readonly ICaptionFormatter captionFormatter; | |
11 | - | |
12 | - public UserDialogProxy(ICaptionFormatter captionFormatter) | |
13 | - { | |
14 | - this.captionFormatter = captionFormatter; | |
15 | - } | |
16 | - | |
17 | - public void ShowInfo(string msg) | |
18 | - { | |
19 | - ShowInfo(msg, null); | |
20 | - } | |
21 | - | |
22 | - public void ShowInfo(string msg, DlgInfo uiInfo) | |
23 | - { | |
24 | - string caption = captionFormatter.GetInfoCaption(uiInfo); | |
25 | - MessageBoxIcon icon = MessageBoxIcon.Information; | |
26 | - | |
27 | - ShowNotify(caption, msg, icon); | |
28 | - } | |
29 | - | |
30 | - public void ShowWarn(string msg) | |
31 | - { | |
32 | - ShowWarn(msg, null); | |
33 | - } | |
34 | - | |
35 | - public void ShowWarn(string msg, DlgInfo uiInfo) | |
36 | - { | |
37 | - string caption = captionFormatter.GetWarnCaption(uiInfo); | |
38 | - MessageBoxIcon icon = MessageBoxIcon.Warning; | |
39 | - | |
40 | - ShowNotify(caption, msg, icon); | |
41 | - } | |
42 | - | |
43 | - public void ShowError(Exception e) | |
44 | - { | |
45 | - ShowError(e, null); | |
46 | - } | |
47 | - | |
48 | - public void ShowError(Exception e, DlgInfo uiInfo) | |
49 | - { | |
50 | - string caption = captionFormatter.GetExceptionCaption(e, uiInfo); | |
51 | - MessageBoxIcon icon = MessageBoxIcon.Error; | |
52 | - | |
53 | - ShowNotify(caption, e.Message, icon); | |
54 | - } | |
55 | - | |
56 | - public void ShowError(string msg) | |
57 | - { | |
58 | - ShowError(msg, null); | |
59 | - } | |
60 | - | |
61 | - public void ShowError(string msg, DlgInfo uiInfo) | |
62 | - { | |
63 | - string caption = captionFormatter.GetErrorCaption(uiInfo); | |
64 | - MessageBoxIcon icon = MessageBoxIcon.Error; | |
65 | - | |
66 | - ShowNotify(caption, msg, icon); | |
67 | - } | |
68 | - | |
69 | - public static void ShowNotify(string caption, string msg, MessageBoxIcon icon) | |
70 | - { | |
71 | - MessageBox.Show(msg, caption, MessageBoxButtons.OK, icon); | |
72 | - } | |
73 | - } | |
74 | -} |
@@ -33,6 +33,7 @@ | ||
33 | 33 | <WarningLevel>4</WarningLevel> |
34 | 34 | </PropertyGroup> |
35 | 35 | <ItemGroup> |
36 | + <Reference Include="PresentationFramework" /> | |
36 | 37 | <Reference Include="System" /> |
37 | 38 | <Reference Include="System.ComponentModel" /> |
38 | 39 | <Reference Include="System.ComponentModel.DataAnnotations" /> |
@@ -75,7 +76,7 @@ | ||
75 | 76 | <Compile Include="Adaptor\Boundary\Controller\Router\IAsyncUseCaseRouterAwareInteractor.cs" /> |
76 | 77 | <Compile Include="Adaptor\Boundary\Controller\Router\IUseCaseRouter.cs" /> |
77 | 78 | <Compile Include="Adaptor\Boundary\Controller\Router\IUseCaseRouterAwareInteractor.cs" /> |
78 | - <Compile Include="Adaptor\Boundary\Gateway\UI\IUserDialogProxy.cs" /> | |
79 | + <Compile Include="Adaptor\Boundary\Gateway\UI\Dialog\IUserDialogProxy.cs" /> | |
79 | 80 | <Compile Include="Adaptor\Controller\AsyncHandlerContextFactory.cs" /> |
80 | 81 | <Compile Include="Adaptor\Controller\HandlerContextFactory.cs" /> |
81 | 82 | <Compile Include="Adaptor\Controller\Handler\AbstractErrorHandler.cs" /> |
@@ -86,8 +87,7 @@ | ||
86 | 87 | <Compile Include="Adaptor\Controller\Router\AbstractUseCaseRouter.cs" /> |
87 | 88 | <Compile Include="Adaptor\Controller\Router\AsyncUseCaseRouter.cs" /> |
88 | 89 | <Compile Include="Adaptor\Controller\Router\UseCaseRouter.cs" /> |
89 | - <Compile Include="Adaptor\Gateway\UI\DlgInfo.cs" /> | |
90 | - <Compile Include="Adaptor\Gateway\UserDialogProxy.cs" /> | |
90 | + <Compile Include="Adaptor\Gateway\UI\Dialog\UserDialogProxy.cs" /> | |
91 | 91 | <Compile Include="CleanAuLait48Const.cs" /> |
92 | 92 | <Compile Include="Core\Async\AbstractCancellationTokenProvider.cs" /> |
93 | 93 | <Compile Include="Core\Async\CancellationTokenManager.cs" /> |
@@ -116,9 +116,16 @@ | ||
116 | 116 | <Compile Include="Core\DI\SimpleInjector\SimpleInjectorComponentProvider.cs" /> |
117 | 117 | <Compile Include="Core\DI\SimpleInjector\SimpleInjectorComponentRegistry.cs" /> |
118 | 118 | <Compile Include="Core\Event\EventWrapper.cs" /> |
119 | - <Compile Include="Core\Helper\DateTimeHelper.cs" /> | |
119 | + <Compile Include="Core\Log\AsyncOnLogTarget.cs" /> | |
120 | + <Compile Include="Core\Log\IAsyncOnLogTarget.cs" /> | |
121 | + <Compile Include="Core\Log\IOnLogTarget.cs" /> | |
122 | + <Compile Include="Core\Log\OnLogTarget.cs" /> | |
123 | + <Compile Include="Core\Log\SimpleLoggerLayoutRenderer.cs" /> | |
124 | + <Compile Include="Core\Time\DateTimeHelper.cs" /> | |
120 | 125 | <Compile Include="Core\Converter\JsonHelper.cs" /> |
126 | + <Compile Include="Core\IO\PathHelper.cs" /> | |
121 | 127 | <Compile Include="Core\Log\LogHelper.cs" /> |
128 | + <Compile Include="Core\Resource\AbstractMessageRepository.cs" /> | |
122 | 129 | <Compile Include="Core\Resource\IMessageRepository.cs" /> |
123 | 130 | <Compile Include="Core\Resource\MessageRepository.cs" /> |
124 | 131 | <Compile Include="Core\Resource\ResourceHelper.cs" /> |
@@ -134,7 +141,6 @@ | ||
134 | 141 | <Compile Include="UseCase\Boundary\Interactor\IUseCaseRouterAware.cs" /> |
135 | 142 | <Compile Include="UseCase\Boundary\Presenter\IAsyncPresenter.cs" /> |
136 | 143 | <Compile Include="UseCase\Boundary\Presenter\IPresenter.cs" /> |
137 | - <Compile Include="UseCase\Request\UIUseCaseRequest.cs" /> | |
138 | 144 | <Compile Include="UseCase\Request\UseCaseRequest.cs" /> |
139 | 145 | <Compile Include="UseCase\Response\UseCaseResponse.cs" /> |
140 | 146 | <Compile Include="UseCase\Response\UseCaseResultTypes.cs" /> |
@@ -150,5 +156,6 @@ | ||
150 | 156 | <Version>5.3.3</Version> |
151 | 157 | </PackageReference> |
152 | 158 | </ItemGroup> |
159 | + <ItemGroup /> | |
153 | 160 | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
154 | 161 | </Project> |
\ No newline at end of file |
@@ -7,7 +7,7 @@ namespace CleanAuLait48.Core.Converter | ||
7 | 7 | /// <see href="https://mokake.hatenablog.com/entry/2017/09/12/195656"> |
8 | 8 | public static class JsonHelper |
9 | 9 | { |
10 | - public static string ToJToJsonStringson<T>(this T data) | |
10 | + public static string ToJsonString<T>(this T data) | |
11 | 11 | { |
12 | 12 | using (var stream = new MemoryStream()) |
13 | 13 | { |
@@ -0,0 +1,48 @@ | ||
1 | +using System.IO; | |
2 | +using System.Linq; | |
3 | + | |
4 | +namespace CleanAuLait48.Core.IO | |
5 | +{ | |
6 | + public static class PathHelper | |
7 | + { | |
8 | + public static readonly string RELATIVE_PATH_CURRENT = "."; | |
9 | + public static readonly string RELATIVE_PATH_UPWARDS = ".."; | |
10 | + | |
11 | + public static string CreateCanonicalPath(params string[] paths) | |
12 | + { | |
13 | + if (paths.Any(path => string.IsNullOrEmpty(path))) | |
14 | + { | |
15 | + return null; | |
16 | + } | |
17 | + | |
18 | + return CreateCanonicalPath(Path.Combine(paths)); | |
19 | + } | |
20 | + | |
21 | + public static string CreateCanonicalPath(string path) | |
22 | + { | |
23 | + if (string.IsNullOrEmpty(path)) | |
24 | + { | |
25 | + return null; | |
26 | + } | |
27 | + | |
28 | + return Path.GetFullPath(path); | |
29 | + } | |
30 | + | |
31 | + public static string EnsureLeadingDotPath(string relativePath) | |
32 | + { | |
33 | + return relativePath == RELATIVE_PATH_CURRENT | |
34 | + ? relativePath | |
35 | + : Path.Combine(RELATIVE_PATH_CURRENT, relativePath); | |
36 | + } | |
37 | + | |
38 | + public static string EnsureEndingDirectorySeparator(string path) | |
39 | + { | |
40 | + if (!path.EndsWith(Path.DirectorySeparatorChar.ToString())) | |
41 | + { | |
42 | + return path + Path.DirectorySeparatorChar; | |
43 | + } | |
44 | + | |
45 | + return path; | |
46 | + } | |
47 | + } | |
48 | +} |
@@ -0,0 +1,127 @@ | ||
1 | +using NLog; | |
2 | +using NLog.Config; | |
3 | +using NLog.Targets; | |
4 | +using NLog.Targets.Wrappers; | |
5 | +using System; | |
6 | +using System.Collections.Generic; | |
7 | +using System.Text; | |
8 | +using System.Threading; | |
9 | +using System.Threading.Tasks; | |
10 | + | |
11 | +namespace CleanAuLait48.Core.Log | |
12 | +{ | |
13 | + public class AsyncOnLogTarget : AsyncTaskTarget, IAsyncOnLogTarget | |
14 | + { | |
15 | + public event EventHandler<string> OnLog; | |
16 | + | |
17 | + private LoggingRule loggingRule; | |
18 | + private bool isClosed; | |
19 | + | |
20 | + public AsyncOnLogTarget(string name, string minLevel) : this(name, ParseLogLevel(minLevel, LogLevel.Info)) | |
21 | + { | |
22 | + } | |
23 | + | |
24 | + public AsyncOnLogTarget(string name, LogLevel minLevel) | |
25 | + { | |
26 | + this.BatchSize = 10; | |
27 | + this.TaskDelayMilliseconds = 200; | |
28 | + this.QueueLimit = 10000; | |
29 | + this.OverflowAction = AsyncTargetWrapperOverflowAction.Discard; | |
30 | + | |
31 | + // important: we want LogManager.Configuration property assign behaviors \ magic to occur | |
32 | + // see: https://stackoverflow.com/a/3603571/1366179 | |
33 | + var config = LogManager.Configuration; | |
34 | + | |
35 | + // Add Target and Rule to their respective collections | |
36 | + config.AddTarget(name, this); | |
37 | + | |
38 | + this.loggingRule = new LoggingRule("*", minLevel, LogLevel.Fatal, this); | |
39 | + config.LoggingRules.Add(this.loggingRule); | |
40 | + | |
41 | + LogManager.Configuration = config; | |
42 | + } | |
43 | + | |
44 | + public void ChangeMinLogLevel(string minLevel) | |
45 | + { | |
46 | + var config = LogManager.Configuration; | |
47 | + | |
48 | + this.loggingRule.Targets.Remove(this); | |
49 | + config.LoggingRules.Remove(this.loggingRule); | |
50 | + | |
51 | + this.loggingRule = new LoggingRule("*", ParseLogLevel(minLevel, LogLevel.Info), LogLevel.Fatal, this); | |
52 | + | |
53 | + config.LoggingRules.Add(this.loggingRule); | |
54 | + | |
55 | + LogManager.Configuration = config; | |
56 | + } | |
57 | + | |
58 | + private static LogLevel ParseLogLevel(string level, LogLevel dafaultValue) | |
59 | + { | |
60 | + try | |
61 | + { | |
62 | + return !string.IsNullOrEmpty(level) ? LogLevel.FromString(level) : dafaultValue; | |
63 | + } | |
64 | + catch | |
65 | + { | |
66 | + return default; | |
67 | + } | |
68 | + } | |
69 | + | |
70 | + public void Close() | |
71 | + { | |
72 | + isClosed = true; | |
73 | + | |
74 | + var config = LogManager.Configuration; | |
75 | + | |
76 | + this.loggingRule.Targets.Remove(this); | |
77 | + config.LoggingRules.Remove(this.loggingRule); | |
78 | + | |
79 | + LogManager.Configuration = config; | |
80 | + } | |
81 | + | |
82 | + protected override Task WriteAsyncTask(LogEventInfo logEvent, CancellationToken cancellationToken) | |
83 | + { | |
84 | + return Task.Run(() => | |
85 | + { | |
86 | + if (isClosed) | |
87 | + { | |
88 | + return; | |
89 | + } | |
90 | + | |
91 | + string msg = Layout.Render(logEvent); | |
92 | + | |
93 | + if (cancellationToken.IsCancellationRequested) | |
94 | + { | |
95 | + return; | |
96 | + } | |
97 | + | |
98 | + OnLog(this, msg); | |
99 | + | |
100 | + }, cancellationToken); | |
101 | + } | |
102 | + | |
103 | + protected override Task WriteAsyncTask(IList<LogEventInfo> logEvents, CancellationToken cancellationToken) | |
104 | + { | |
105 | + return Task.Run(() => | |
106 | + { | |
107 | + if (isClosed) | |
108 | + { | |
109 | + return; | |
110 | + } | |
111 | + | |
112 | + StringBuilder buf = new StringBuilder(); | |
113 | + foreach (var logEvent in logEvents) | |
114 | + { | |
115 | + buf.AppendLine(Layout.Render(logEvent)); | |
116 | + } | |
117 | + | |
118 | + if (cancellationToken.IsCancellationRequested) | |
119 | + { | |
120 | + return; | |
121 | + } | |
122 | + | |
123 | + OnLog(this, buf.ToString()); | |
124 | + }, cancellationToken); | |
125 | + } | |
126 | + } | |
127 | +} |
@@ -0,0 +1,11 @@ | ||
1 | +using System; | |
2 | + | |
3 | +namespace CleanAuLait48.Core.Log | |
4 | +{ | |
5 | + public interface IAsyncOnLogTarget | |
6 | + { | |
7 | + event EventHandler<string> OnLog; | |
8 | + void ChangeMinLogLevel(string minLevel); | |
9 | + void Close(); | |
10 | + } | |
11 | +} | |
\ No newline at end of file |
@@ -0,0 +1,11 @@ | ||
1 | +using System; | |
2 | + | |
3 | +namespace CleanAuLait48.Core.Log | |
4 | +{ | |
5 | + public interface IOnLogTarget | |
6 | + { | |
7 | + event EventHandler<string> OnLog; | |
8 | + void ChangeMinLogLevel(string minLevel); | |
9 | + void Close(); | |
10 | + } | |
11 | +} | |
\ No newline at end of file |
@@ -0,0 +1,127 @@ | ||
1 | +using NLog; | |
2 | +using NLog.Common; | |
3 | +using NLog.Config; | |
4 | +using NLog.Targets; | |
5 | +using NLog.Targets.Wrappers; | |
6 | +using System; | |
7 | +using System.Collections.Generic; | |
8 | +using System.Text; | |
9 | + | |
10 | +namespace CleanAuLait48.Core.Log | |
11 | +{ | |
12 | + public class OnLogTarget : TargetWithLayout, IOnLogTarget | |
13 | + { | |
14 | + public event EventHandler<string> OnLog; | |
15 | + | |
16 | + private LoggingRule loggingRule; | |
17 | + private bool isClosed; | |
18 | + | |
19 | + private readonly object objLock = new object(); | |
20 | + | |
21 | + public OnLogTarget(string name, string minLevel) | |
22 | + { | |
23 | + // important: we want LogManager.Configuration property assign behaviors \ magic to occur | |
24 | + // see: https://stackoverflow.com/a/3603571/1366179 | |
25 | + var config = LogManager.Configuration; | |
26 | + | |
27 | + Target target = new AsyncTargetWrapper(this, 8192, AsyncTargetWrapperOverflowAction.Discard); | |
28 | + | |
29 | + // Add Target and Rule to their respective collections | |
30 | + config.AddTarget(name, target); | |
31 | + | |
32 | + this.loggingRule = new LoggingRule("*", ParseLogLevel(minLevel, LogLevel.Info), LogLevel.Fatal, this); | |
33 | + config.LoggingRules.Add(this.loggingRule); | |
34 | + | |
35 | + LogManager.Configuration = config; | |
36 | + } | |
37 | + | |
38 | + public void ChangeMinLogLevel(string minLevel) | |
39 | + { | |
40 | + var config = LogManager.Configuration; | |
41 | + | |
42 | + this.loggingRule.Targets.Remove(this); | |
43 | + config.LoggingRules.Remove(this.loggingRule); | |
44 | + | |
45 | + this.loggingRule = new LoggingRule("*", ParseLogLevel(minLevel, LogLevel.Info), LogLevel.Fatal, this); | |
46 | + | |
47 | + config.LoggingRules.Add(this.loggingRule); | |
48 | + | |
49 | + LogManager.Configuration = config; | |
50 | + } | |
51 | + | |
52 | + private static LogLevel ParseLogLevel(string level, LogLevel dafaultValue) | |
53 | + { | |
54 | + try | |
55 | + { | |
56 | + return !string.IsNullOrEmpty(level) ? LogLevel.FromString(level) : dafaultValue; | |
57 | + } | |
58 | + catch | |
59 | + { | |
60 | + return default; | |
61 | + } | |
62 | + } | |
63 | + | |
64 | + public void Close() | |
65 | + { | |
66 | + isClosed = true; | |
67 | + | |
68 | + var config = LogManager.Configuration; | |
69 | + | |
70 | + this.loggingRule.Targets.Remove(this); | |
71 | + config.LoggingRules.Remove(this.loggingRule); | |
72 | + | |
73 | + LogManager.Configuration = config; | |
74 | + } | |
75 | + | |
76 | + protected override void Write(LogEventInfo logEvent) | |
77 | + { | |
78 | + if (isClosed) | |
79 | + { | |
80 | + return; | |
81 | + } | |
82 | + | |
83 | + string msg = Layout.Render(logEvent); | |
84 | + | |
85 | + OnLog(this, msg); | |
86 | + } | |
87 | + | |
88 | + protected override void Write(AsyncLogEventInfo logEvent) | |
89 | + { | |
90 | + Write(logEvent.LogEvent); | |
91 | + } | |
92 | + | |
93 | + protected override void WriteAsyncThreadSafe(AsyncLogEventInfo logEvent) | |
94 | + { | |
95 | + lock (objLock) | |
96 | + { | |
97 | + Write(logEvent); | |
98 | + } | |
99 | + } | |
100 | + | |
101 | + protected override void Write(IList<AsyncLogEventInfo> logEvents) | |
102 | + { | |
103 | + StringBuilder buf = new StringBuilder(); | |
104 | + | |
105 | + foreach (AsyncLogEventInfo logEvent in logEvents) | |
106 | + { | |
107 | + buf.AppendLine(Layout.Render(logEvent.LogEvent)); | |
108 | + } | |
109 | + | |
110 | + OnLog(this, buf.ToString()); | |
111 | + } | |
112 | + | |
113 | + protected override void WriteAsyncThreadSafe(IList<AsyncLogEventInfo> logEvents) | |
114 | + { | |
115 | + lock (objLock) | |
116 | + { | |
117 | + if (isClosed) | |
118 | + { | |
119 | + return; | |
120 | + } | |
121 | + | |
122 | + Write(logEvents); | |
123 | + } | |
124 | + } | |
125 | + | |
126 | + } | |
127 | +} |
@@ -0,0 +1,17 @@ | ||
1 | +using NLog; | |
2 | +using NLog.LayoutRenderers; | |
3 | +using System.Text; | |
4 | + | |
5 | +namespace CleanAuLait48.Core.Log | |
6 | +{ | |
7 | + public class SimpleLoggerLayoutRenderer : LayoutRenderer | |
8 | + { | |
9 | + protected override void Append(StringBuilder builder, LogEventInfo logEvent) | |
10 | + { | |
11 | + string loggerName = logEvent.LoggerName; | |
12 | + string simpleLoggerName = loggerName.Substring(loggerName.LastIndexOf('.') + 1); | |
13 | + | |
14 | + builder.Append(simpleLoggerName); | |
15 | + } | |
16 | + } | |
17 | +} |
@@ -0,0 +1,42 @@ | ||
1 | +using System.Collections.Generic; | |
2 | + | |
3 | +namespace CleanAuLait48.Core.Resource | |
4 | +{ | |
5 | + public abstract class AbstractMessageRepository : IMessageRepository | |
6 | + { | |
7 | + protected readonly IDictionary<string, string> map = new Dictionary<string, string>(); | |
8 | + | |
9 | + protected virtual void RegisterMessages(string properties) | |
10 | + { | |
11 | + string[] lines = properties.Split('\n'); | |
12 | + | |
13 | + foreach (string line in lines) | |
14 | + { | |
15 | + if (line.StartsWith("#") || (line.Trim().Length == 0)) | |
16 | + { | |
17 | + continue; | |
18 | + } | |
19 | + | |
20 | + string[] data = line.Split('='); | |
21 | + map.Add(data[0].Trim(), data[1].Trim()); | |
22 | + } | |
23 | + } | |
24 | + | |
25 | + public virtual string Get(string key) | |
26 | + { | |
27 | + return GetMap()[key]; | |
28 | + } | |
29 | + | |
30 | + public virtual string Get(string key, params string[] args) | |
31 | + { | |
32 | + string msg = GetMap()[key]; | |
33 | + | |
34 | + return string.Format(msg, args); | |
35 | + } | |
36 | + | |
37 | + public virtual IDictionary<string, string> GetMap() | |
38 | + { | |
39 | + return map; | |
40 | + } | |
41 | + } | |
42 | +} |
@@ -6,86 +6,116 @@ namespace CleanAuLait48.Core.Resource | ||
6 | 6 | public class CaptionFormatter : ICaptionFormatter |
7 | 7 | { |
8 | 8 | public const string MSG_KEY_CAPTION = "Caption"; |
9 | - public const string MSG_KEY_CAPTION_INFO = "Caption.Info"; | |
10 | - public const string MSG_KEY_CAPTION_WARN = "Caption.Warn"; | |
11 | - public const string MSG_KEY_CAPTION_ERROR = "Caption.Error"; | |
12 | - public const string MSG_KEY_CAPTION_EXCEPTION = "Caption.Exception"; | |
13 | - | |
9 | + | |
10 | + public const string MSG_KEY_PREFIX_CAPTION_FORMAT = "Caption.Format."; | |
11 | + | |
12 | + public const string MSG_KEY_CAPTION_FORMAT_INFO = MSG_KEY_PREFIX_CAPTION_FORMAT + "Info"; | |
13 | + public const string MSG_KEY_CAPTION_FORMAT_WARN = MSG_KEY_PREFIX_CAPTION_FORMAT + "Warn"; | |
14 | + public const string MSG_KEY_CAPTION_FORMAT_ERROR = MSG_KEY_PREFIX_CAPTION_FORMAT + "Error"; | |
15 | + public const string MSG_KEY_CAPTION_FORMAT_CONFIRM = MSG_KEY_PREFIX_CAPTION_FORMAT + "Confirm"; | |
16 | + public const string MSG_KEY_CAPTION_FORMAT_EXCEPTION = MSG_KEY_PREFIX_CAPTION_FORMAT + "Exception"; | |
17 | + | |
14 | 18 | protected readonly IMessageRepository repo; |
15 | - protected readonly DlgInfo defaultDlgInfo; | |
16 | 19 | |
17 | 20 | public CaptionFormatter(IMessageRepository repo) |
18 | 21 | { |
19 | 22 | this.repo = repo; |
20 | - this.defaultDlgInfo = new DlgInfo(GetCaption()); | |
21 | 23 | } |
22 | 24 | |
23 | - public string GetCaption() | |
25 | + public string GetDefaultCaption() | |
24 | 26 | { |
25 | - return repo.Get(MSG_KEY_CAPTION); | |
27 | + return GetCaption(MSG_KEY_CAPTION); | |
26 | 28 | } |
27 | 29 | |
28 | - public DlgInfo GetDefaultDlgInfo() | |
30 | + public string GetCaption(string captionKey) | |
29 | 31 | { |
30 | - return defaultDlgInfo; | |
32 | + return this.repo.Get(captionKey); | |
31 | 33 | } |
32 | 34 | |
33 | 35 | public string GetInfoCaption() |
34 | 36 | { |
35 | - return GetCaption(MSG_KEY_CAPTION_INFO); | |
37 | + return FormatDefaultCaption(MSG_KEY_CAPTION_FORMAT_INFO); | |
36 | 38 | } |
37 | 39 | |
38 | - public string GetInfoCaption(DlgInfo dlgInfo) | |
40 | + public string FormatInfoCaption(string captionKey) | |
39 | 41 | { |
40 | - return GetCaption(MSG_KEY_CAPTION_INFO, dlgInfo); | |
42 | + return FormatCaption(MSG_KEY_CAPTION_FORMAT_INFO, captionKey); | |
41 | 43 | } |
42 | 44 | |
43 | 45 | public string GetWarnCaption() |
44 | 46 | { |
45 | - return GetCaption(MSG_KEY_CAPTION_WARN); | |
47 | + return FormatDefaultCaption(MSG_KEY_CAPTION_FORMAT_WARN); | |
46 | 48 | } |
47 | 49 | |
48 | - public string GetWarnCaption(DlgInfo dlgInfo) | |
50 | + public string FormatWarnCaption(string captionKey) | |
49 | 51 | { |
50 | - return GetCaption(MSG_KEY_CAPTION_WARN, dlgInfo); | |
52 | + return FormatCaption(MSG_KEY_CAPTION_FORMAT_WARN, captionKey); | |
51 | 53 | } |
52 | 54 | |
53 | 55 | public string GetErrorCaption() |
54 | 56 | { |
55 | - return GetCaption(MSG_KEY_CAPTION_ERROR); | |
57 | + return FormatDefaultCaption(MSG_KEY_CAPTION_FORMAT_ERROR); | |
58 | + } | |
59 | + | |
60 | + public string FormatErrorCaption(string captionKey) | |
61 | + { | |
62 | + return FormatCaption(MSG_KEY_CAPTION_FORMAT_ERROR, captionKey); | |
56 | 63 | } |
57 | 64 | |
58 | - public string GetErrorCaption(DlgInfo dlgInfo) | |
65 | + public string GetConfirmCaption() | |
59 | 66 | { |
60 | - return GetCaption(MSG_KEY_CAPTION_ERROR, dlgInfo); | |
67 | + return FormatDefaultCaption(MSG_KEY_CAPTION_FORMAT_ERROR); | |
68 | + } | |
69 | + | |
70 | + public string FormatConfirmCaption(string captionKey) | |
71 | + { | |
72 | + return FormatCaption(MSG_KEY_CAPTION_FORMAT_CONFIRM, captionKey); | |
61 | 73 | } |
62 | 74 | |
63 | 75 | public string GetExceptionCaption(Exception e) |
64 | 76 | { |
65 | - return GetExceptionCaption(e, null); | |
77 | + return FormatExceptionCaption(e, null); | |
66 | 78 | } |
67 | 79 | |
68 | - public string GetExceptionCaption(Exception e, DlgInfo dlgInfo) | |
80 | + public string FormatExceptionCaption(Exception e, string captionKey) | |
69 | 81 | { |
70 | - if (dlgInfo == null) | |
82 | + string caption; | |
83 | + | |
84 | + if (string.IsNullOrEmpty(captionKey)) | |
71 | 85 | { |
72 | - dlgInfo = defaultDlgInfo; | |
86 | + caption = GetDefaultCaption(); | |
73 | 87 | } |
74 | - return string.Format(repo.Get(MSG_KEY_CAPTION_EXCEPTION), dlgInfo.Caption, e.GetType().Name); | |
88 | + else | |
89 | + { | |
90 | + caption = GetCaption(captionKey); | |
91 | + } | |
92 | + | |
93 | + string format = GetCaption(MSG_KEY_CAPTION_FORMAT_EXCEPTION); | |
94 | + | |
95 | + return string.Format(format, caption, e.GetType().Name); | |
75 | 96 | } |
76 | 97 | |
77 | - protected string GetCaption(string msgCaptionKey) | |
98 | + protected string FormatDefaultCaption(string captionFormatKey) | |
78 | 99 | { |
79 | - return GetCaption(msgCaptionKey, null); | |
100 | + return FormatCaption(captionFormatKey, null); | |
80 | 101 | } |
81 | 102 | |
82 | - protected string GetCaption(string msgCaptionKey, DlgInfo dlgInfo) | |
103 | + protected string FormatCaption(string captionFormatKey, string captionKey) | |
83 | 104 | { |
84 | - if (dlgInfo == null) | |
105 | + string caption; | |
106 | + | |
107 | + if (string.IsNullOrEmpty(captionKey)) | |
85 | 108 | { |
86 | - dlgInfo = defaultDlgInfo; | |
109 | + caption = GetDefaultCaption(); | |
87 | 110 | } |
88 | - return string.Format(repo.Get(msgCaptionKey), dlgInfo.Caption); | |
111 | + else | |
112 | + { | |
113 | + caption = GetCaption(captionKey); | |
114 | + } | |
115 | + | |
116 | + string format = GetCaption(captionFormatKey); | |
117 | + | |
118 | + return string.Format(format, caption); | |
89 | 119 | } |
90 | 120 | |
91 | 121 | } |
@@ -1,19 +1,20 @@ | ||
1 | -using CleanAuLait48.Adaptor.Gateway.UI; | |
2 | -using System; | |
1 | +using System; | |
3 | 2 | |
4 | 3 | namespace CleanAuLait48.Core.Resource |
5 | 4 | { |
6 | 5 | public interface ICaptionFormatter |
7 | 6 | { |
8 | - string GetCaption(); | |
9 | - DlgInfo GetDefaultDlgInfo(); | |
10 | - string GetErrorCaption(); | |
11 | - string GetErrorCaption(DlgInfo dlgInfo); | |
12 | - string GetExceptionCaption(Exception e); | |
13 | - string GetExceptionCaption(Exception e, DlgInfo dlgInfo); | |
7 | + string GetDefaultCaption(); | |
8 | + string GetCaption(string captionKey); | |
14 | 9 | string GetInfoCaption(); |
15 | - string GetInfoCaption(DlgInfo dlgInfo); | |
10 | + string FormatInfoCaption(string captionKey); | |
16 | 11 | string GetWarnCaption(); |
17 | - string GetWarnCaption(DlgInfo dlgInfo); | |
12 | + string FormatWarnCaption(string captionKey); | |
13 | + string GetErrorCaption(); | |
14 | + string FormatErrorCaption(string captionKey); | |
15 | + string GetConfirmCaption(); | |
16 | + string FormatConfirmCaption(string captionKey); | |
17 | + string GetExceptionCaption(Exception e); | |
18 | + string FormatExceptionCaption(Exception e, string captionKey); | |
18 | 19 | } |
19 | 20 | } |
\ No newline at end of file |
@@ -1,12 +1,9 @@ | ||
1 | -using System.Collections.Generic; | |
2 | -using System.Reflection; | |
1 | +using System.Reflection; | |
3 | 2 | |
4 | 3 | namespace CleanAuLait48.Core.Resource |
5 | 4 | { |
6 | - public class MessageRepository : IMessageRepository | |
5 | + public class MessageRepository : AbstractMessageRepository | |
7 | 6 | { |
8 | - private readonly IDictionary<string, string> map = new Dictionary<string, string>(); | |
9 | - | |
10 | 7 | public MessageRepository(string nameSpace, string path, Assembly asm) |
11 | 8 | { |
12 | 9 | ReadMessageResource(nameSpace, path, asm); |
@@ -29,17 +26,5 @@ namespace CleanAuLait48.Core.Resource | ||
29 | 26 | map.Add(data[0].Trim(), data[1].Trim()); |
30 | 27 | } |
31 | 28 | } |
32 | - | |
33 | - public string Get(string key) | |
34 | - { | |
35 | - return map[key]; | |
36 | - } | |
37 | - | |
38 | - public string Get(string key, params string[] args) | |
39 | - { | |
40 | - string msg = map[key]; | |
41 | - | |
42 | - return string.Format(msg, args); | |
43 | - } | |
44 | 29 | } |
45 | 30 | } |
@@ -1,6 +1,6 @@ | ||
1 | 1 | using System; |
2 | 2 | |
3 | -namespace CleanAuLait48.Core.Helper | |
3 | +namespace CleanAuLait48.Core.Time | |
4 | 4 | { |
5 | 5 | public static class DateTimeHelper |
6 | 6 | { |
@@ -10,6 +10,13 @@ namespace CleanAuLait48.Core.Helper | ||
10 | 10 | |
11 | 11 | public static DateTime JstNow => TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TZ_JST); |
12 | 12 | |
13 | - public static long ToUnixTime(DateTime time) => (long)((time - UNIX_EPOCH).TotalSeconds); | |
13 | +#if NET7_0 | |
14 | + public static long ToUnixTime(DateTime time) => (long)((time - UNIX_EPOCH).TotalSeconds) * 1000000 + time.Microsecond; | |
15 | +#else | |
16 | + public static long ToUnixTime(DateTime time) | |
17 | + { | |
18 | + return (long) ((time - UNIX_EPOCH).TotalSeconds) * 1000000 + long.Parse(time.ToString("ffffff")); | |
19 | + } | |
20 | +#endif | |
14 | 21 | } |
15 | 22 | } |
@@ -1,18 +0,0 @@ | ||
1 | -using CleanAuLait48.Adaptor.Gateway.UI; | |
2 | - | |
3 | -namespace CleanAuLait48.UseCase.Request | |
4 | -{ | |
5 | - public abstract class UIUseCaseRequest : UseCaseRequest | |
6 | - { | |
7 | - public DlgInfo UIInfo { get; set; } | |
8 | - | |
9 | - public UIUseCaseRequest(string caption) : this(new DlgInfo(caption)) | |
10 | - { | |
11 | - } | |
12 | - | |
13 | - public UIUseCaseRequest(DlgInfo uiInfo) | |
14 | - { | |
15 | - this.UIInfo = uiInfo; | |
16 | - } | |
17 | - } | |
18 | -} |