Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
netcoreplus
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
netcoreplus
Commits
d24fe49b
Commit
d24fe49b
authored
May 16, 2019
by
阿星Plus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
LogHelper
parent
e6f8f8b9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
0 deletions
+149
-0
LogHelper.cs
src/Plus/Logging/LogHelper.cs
+69
-0
LoggerExtensions.cs
src/Plus/Logging/LoggerExtensions.cs
+80
-0
No files found.
src/Plus/Logging/LogHelper.cs
0 → 100644
View file @
d24fe49b
using
Castle.Core.Logging
;
using
Plus.Dependency
;
using
Plus.Runtime.Validation
;
using
System
;
using
System.Linq
;
namespace
Plus.Logging
{
public
static
class
LogHelper
{
public
static
ILogger
Logger
{
get
;
private
set
;
}
static
LogHelper
()
{
Logger
=
IocManager
.
Instance
.
IsRegistered
(
typeof
(
ILoggerFactory
))
?
IocManager
.
Instance
.
Resolve
<
ILoggerFactory
>().
Create
(
typeof
(
LogHelper
))
:
NullLogger
.
Instance
;
}
public
static
void
LogException
(
Exception
ex
)
{
LogException
(
Logger
,
ex
);
}
public
static
void
LogException
(
ILogger
logger
,
Exception
ex
)
{
LogSeverity
severity
=
(
ex
as
IHasLogSeverity
)?.
Severity
??
LogSeverity
.
Error
;
logger
.
Log
(
severity
,
ex
.
Message
,
ex
);
LogValidationErrors
(
logger
,
ex
);
}
private
static
void
LogValidationErrors
(
ILogger
logger
,
Exception
exception
)
{
//Try to find inner validation exception
if
(
exception
is
AggregateException
&&
exception
.
InnerException
!=
null
)
{
var
aggException
=
exception
as
AggregateException
;
if
(
aggException
.
InnerException
is
PlusValidationException
)
{
exception
=
aggException
.
InnerException
;
}
}
if
(!(
exception
is
PlusValidationException
))
{
return
;
}
var
validationException
=
exception
as
PlusValidationException
;
if
(
validationException
.
ValidationErrors
.
IsNullOrEmpty
())
{
return
;
}
logger
.
Log
(
validationException
.
Severity
,
"There are "
+
validationException
.
ValidationErrors
.
Count
+
" validation errors:"
);
foreach
(
var
validationResult
in
validationException
.
ValidationErrors
)
{
var
memberNames
=
""
;
if
(
validationResult
.
MemberNames
!=
null
&&
validationResult
.
MemberNames
.
Any
())
{
memberNames
=
" ("
+
string
.
Join
(
", "
,
validationResult
.
MemberNames
)
+
")"
;
}
logger
.
Log
(
validationException
.
Severity
,
validationResult
.
ErrorMessage
+
memberNames
);
}
}
}
}
\ No newline at end of file
src/Plus/Logging/LoggerExtensions.cs
0 → 100644
View file @
d24fe49b
using
Castle.Core.Logging
;
using
System
;
namespace
Plus.Logging
{
public
static
class
LoggerExtensions
{
public
static
void
Log
(
this
ILogger
logger
,
LogSeverity
severity
,
string
message
)
{
switch
(
severity
)
{
case
LogSeverity
.
Fatal
:
logger
.
Fatal
(
message
);
break
;
case
LogSeverity
.
Error
:
logger
.
Error
(
message
);
break
;
case
LogSeverity
.
Warn
:
logger
.
Warn
(
message
);
break
;
case
LogSeverity
.
Info
:
logger
.
Info
(
message
);
break
;
case
LogSeverity
.
Debug
:
logger
.
Debug
(
message
);
break
;
default
:
throw
new
PlusException
(
"Unknown LogSeverity value: "
+
severity
);
}
}
public
static
void
Log
(
this
ILogger
logger
,
LogSeverity
severity
,
string
message
,
Exception
exception
)
{
switch
(
severity
)
{
case
LogSeverity
.
Fatal
:
logger
.
Fatal
(
message
,
exception
);
break
;
case
LogSeverity
.
Error
:
logger
.
Error
(
message
,
exception
);
break
;
case
LogSeverity
.
Warn
:
logger
.
Warn
(
message
,
exception
);
break
;
case
LogSeverity
.
Info
:
logger
.
Info
(
message
,
exception
);
break
;
case
LogSeverity
.
Debug
:
logger
.
Debug
(
message
,
exception
);
break
;
default
:
throw
new
PlusException
(
"Unknown LogSeverity value: "
+
severity
);
}
}
public
static
void
Log
(
this
ILogger
logger
,
LogSeverity
severity
,
Func
<
string
>
messageFactory
)
{
switch
(
severity
)
{
case
LogSeverity
.
Fatal
:
logger
.
Fatal
(
messageFactory
);
break
;
case
LogSeverity
.
Error
:
logger
.
Error
(
messageFactory
);
break
;
case
LogSeverity
.
Warn
:
logger
.
Warn
(
messageFactory
);
break
;
case
LogSeverity
.
Info
:
logger
.
Info
(
messageFactory
);
break
;
case
LogSeverity
.
Debug
:
logger
.
Debug
(
messageFactory
);
break
;
default
:
throw
new
PlusException
(
"Unknown LogSeverity value: "
+
severity
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment