feat: add ERP agent pet bridge and startup guide
This commit is contained in:
@@ -31,6 +31,93 @@ namespace Lskj.Util
|
||||
/// </summary>
|
||||
public class LogHelper
|
||||
{
|
||||
[ThreadStatic]
|
||||
private static DiagnosticExceptionObservationScope
|
||||
_diagnosticExceptionObservation;
|
||||
[ThreadStatic]
|
||||
private static bool _observingDiagnosticException;
|
||||
|
||||
/// <summary>
|
||||
/// 在当前线程建立短时异常观察范围。仅供受信任的 ERP 初始化诊断
|
||||
/// 使用;异常对象不会因此写入桥协议,原有日志行为也不会改变。
|
||||
/// </summary>
|
||||
public static IDisposable BeginDiagnosticExceptionObservation(
|
||||
Action<Exception> observer)
|
||||
{
|
||||
if (observer == null)
|
||||
throw new ArgumentNullException("observer");
|
||||
DiagnosticExceptionObservationScope scope =
|
||||
new DiagnosticExceptionObservationScope(
|
||||
observer,
|
||||
_diagnosticExceptionObservation,
|
||||
Thread.CurrentThread.ManagedThreadId);
|
||||
_diagnosticExceptionObservation = scope;
|
||||
return scope;
|
||||
}
|
||||
|
||||
private static void ObserveDiagnosticException(Exception exception)
|
||||
{
|
||||
if (exception == null || _observingDiagnosticException) return;
|
||||
_observingDiagnosticException = true;
|
||||
try
|
||||
{
|
||||
DiagnosticExceptionObservationScope current =
|
||||
_diagnosticExceptionObservation;
|
||||
int depth = 0;
|
||||
while (current != null && depth < 16)
|
||||
{
|
||||
try { current.Observer(exception); }
|
||||
catch { }
|
||||
current = current.Previous;
|
||||
depth += 1;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_observingDiagnosticException = false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class DiagnosticExceptionObservationScope : IDisposable
|
||||
{
|
||||
private readonly int _ownerThreadId;
|
||||
private bool _disposed;
|
||||
|
||||
public DiagnosticExceptionObservationScope(
|
||||
Action<Exception> observer,
|
||||
DiagnosticExceptionObservationScope previous,
|
||||
int ownerThreadId)
|
||||
{
|
||||
Observer = observer;
|
||||
Previous = previous;
|
||||
_ownerThreadId = ownerThreadId;
|
||||
}
|
||||
|
||||
public Action<Exception> Observer { get; private set; }
|
||||
public DiagnosticExceptionObservationScope Previous
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
if (Thread.CurrentThread.ManagedThreadId != _ownerThreadId
|
||||
|| !object.ReferenceEquals(
|
||||
_diagnosticExceptionObservation,
|
||||
this))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"诊断异常观察范围必须在创建线程按嵌套顺序释放。");
|
||||
}
|
||||
_diagnosticExceptionObservation = Previous;
|
||||
Observer = null;
|
||||
Previous = null;
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日志文件存放文件夹
|
||||
/// </summary>
|
||||
@@ -189,7 +276,7 @@ namespace Lskj.Util
|
||||
{
|
||||
Directory.CreateDirectory(_dicPath);
|
||||
}
|
||||
catch(Exception e)
|
||||
catch(Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -230,7 +317,7 @@ namespace Lskj.Util
|
||||
fs.Close();
|
||||
_fileList.Add(Path.GetFileName(filePath));
|
||||
}
|
||||
catch(Exception e)
|
||||
catch(Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -279,7 +366,7 @@ namespace Lskj.Util
|
||||
fs.Close();
|
||||
_fileList.Add(Path.GetFileName(filePath));
|
||||
}
|
||||
catch(Exception e)
|
||||
catch(Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -328,7 +415,7 @@ namespace Lskj.Util
|
||||
streamWriter.WriteLine(writeLog);
|
||||
streamWriter.Flush();
|
||||
}
|
||||
catch(Exception e)
|
||||
catch(Exception)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -398,6 +485,7 @@ namespace Lskj.Util
|
||||
/// <param name="logType">Type of the log.</param>
|
||||
public void WriteError(Exception exception, LogType logType = LogType.Error)
|
||||
{
|
||||
ObserveDiagnosticException(exception);
|
||||
string strLog = "";
|
||||
lock (_objLock)
|
||||
{
|
||||
@@ -422,6 +510,7 @@ namespace Lskj.Util
|
||||
/// <param name="logType">Type of the log.</param>
|
||||
public void WriteError(Exception exception, string title, LogType logType = LogType.Error)
|
||||
{
|
||||
ObserveDiagnosticException(exception);
|
||||
string strLog = "";
|
||||
lock (_objLock)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user