Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventSource NetCore #461

Merged
merged 17 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static private void TraceException(string trace, Exception e)
Debug.Assert(null != e, "TraceException: null Exception");
if (null != e)
{
SqlClientEventSource.Log.TraceEvent(trace, e.ToString());
SqlClientEventSource.Log.TraceEvent(trace, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static private void TraceException(
Debug.Assert(null != e, "TraceException: null Exception");
if (null != e)
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
{
SqlClientEventSource.Log.AdvanceTrace(trace, e.Message);
SqlClientEventSource.Log.AdvanceTrace("<comm.ADP.TraceException|ERR|ADV> Environment StackTrace = '{0}'", Environment.StackTrace);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ virtual internal void DelegatedTransactionEnded()
// IMPORTANT NOTE: You must have taken a lock on the object before
// you call this method to prevent race conditions with Clear and
// ReclaimEmancipatedObjects.
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionInternal.DelegatedTransactionEnded|RES|CPOOL> {0}#, Delegated Transaction Completed.", ObjectID);
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DelegatedTransactionEnded|RES|CPOOL> {0}#, Delegated Transaction Completed.", ObjectID);

if (1 == _pooledCount)
{
Expand Down Expand Up @@ -426,7 +426,7 @@ internal void DetachCurrentTransactionIfEnded()
// Detach transaction from connection.
internal void DetachTransaction(Transaction transaction, bool isExplicitlyReleasing)
{
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionInternal.DetachTransaction|RES|CPOOL> {0}#, Transaction Completed. (pooledCount={1})", ObjectID, _pooledCount);
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DetachTransaction|RES|CPOOL> {0}#, Transaction Completed. (pooledCount={1})", ObjectID, _pooledCount);

// potentially a multi-threaded event, so lock the connection to make sure we don't enlist in a new
// transaction between compare and assignment. No need to short circuit outside of lock, since failed comparisons should
Expand Down Expand Up @@ -466,7 +466,7 @@ internal void CleanupConnectionOnTransactionCompletion(Transaction transaction)
void TransactionCompletedEvent(object sender, TransactionEventArgs e)
{
Transaction transaction = e.Transaction;
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionInternal.TransactionCompletedEvent|RES|CPOOL> {0}#, Transaction Completed. (pooledCount = {1})", ObjectID, _pooledCount);
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.TransactionCompletedEvent|RES|CPOOL> {0}#, Transaction Completed. (pooledCount = {1})", ObjectID, _pooledCount);
CleanupTransactionOnCompletion(transaction);
CleanupConnectionOnTransactionCompletion(transaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ internal void TraceEvent<T0>(string message, T0 args0)
{
if (Log.IsTraceEnabled())
{
Trace(string.Format(message, args0));
Trace(string.Format(message, args0.ToString()));
}
}

Expand All @@ -204,7 +204,7 @@ internal void TraceEvent<T0, T1>(string message, T0 args0, T1 args1)
{
if (Log.IsTraceEnabled())
{
Trace(string.Format(message, args0, args1));
Trace(string.Format(message, args0.ToString(), args1.ToString()));
}
}

Expand All @@ -213,7 +213,7 @@ internal void TraceEvent<T0, T1, T2>(string message, T0 args0, T1 args1, T2 args
{
if (Log.IsTraceEnabled())
{
Trace(string.Format(message, args0, args1, args2));
Trace(string.Format(message, args0.ToString(), args1.ToString(), args2.ToString()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal void Abort(Exception e)
}
else
{
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionHelper.Abort|RES|INFO|CPOOL> {0}#, Aborting operation due to asynchronous exception: {1}", ObjectID, e.ToString());
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionHelper.Abort|RES|INFO|CPOOL> {0}#, Aborting operation due to asynchronous exception: {1}", ObjectID, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sealed internal class LastIOTimer

internal abstract class TdsParserStateObject
{
private static int _objectTypeCount; // Bid counter
private static int _objectTypeCount; // EventSource counter
internal readonly int _objectID = Interlocked.Increment(ref _objectTypeCount);

[Flags]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Threading;
using Microsoft.Data.Common;
using Microsoft.Data.SqlClient;
using Microsoft.Win32.SafeHandles;

namespace Microsoft.Data.SqlTypes
Expand All @@ -21,6 +23,9 @@ public sealed partial class SqlFileStream : System.IO.Stream
// TransactionContext accessors as virtual methods. Doing so now on a sealed class
// generates a compiler error (CS0549)

private static int _objectTypeCount; // Bid counter
internal int ObjectID { get; } = Interlocked.Increment(ref _objectTypeCount);

// from System.IO.FileStream implementation
// DefaultBufferSize = 4096;
// SQLBUVSTS# 193123 - disable lazy flushing of written data in order to prevent
Expand Down Expand Up @@ -56,25 +61,33 @@ public SqlFileStream(string path, byte[] transactionContext, FileAccess access)
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlFileStream.xml' path='docs/members[@name="SqlFileStream"]/ctor2/*' />
public SqlFileStream(string path, byte[] transactionContext, FileAccess access, FileOptions options, long allocationSize)
{
//-----------------------------------------------------------------
// precondition validation
long scopeID = SqlClientEventSource.Log.ScopeEnterEvent("<sc.SqlFileStream.ctor|API> {0}# access={1} options={2} path='{3}'", ObjectID, (int)access, (int)options, path);
try
{
//-----------------------------------------------------------------
// precondition validation

if (transactionContext == null)
throw ADP.ArgumentNull("transactionContext");
if (transactionContext == null)
throw ADP.ArgumentNull("transactionContext");

if (path == null)
throw ADP.ArgumentNull("path");
if (path == null)
throw ADP.ArgumentNull("path");

//-----------------------------------------------------------------
//-----------------------------------------------------------------

_m_disposed = false;
_m_fs = null;
_m_disposed = false;
_m_fs = null;

OpenSqlFileStream(path, transactionContext, access, options, allocationSize);
OpenSqlFileStream(path, transactionContext, access, options, allocationSize);

// only set internal state once the file has actually been successfully opened
Name = path;
TransactionContext = transactionContext;
// only set internal state once the file has actually been successfully opened
Name = path;
TransactionContext = transactionContext;
}
finally
{
SqlClientEventSource.Log.ScopeLeaveEvent(scopeID);
}
}

#region destructor/dispose code
Expand Down Expand Up @@ -585,6 +598,10 @@ long allocationSize
createOptions: dwCreateOptions,
eaBuffer: b,
eaLength: (uint)fullSize);

SqlClientEventSource.Log.AdvanceTrace("<sc.SqlFileStream.OpenSqlFileStream|ADV> {0}#, desiredAccess=0x{1}, allocationSize={2}, " +
"fileAttributes=0x{3}, shareAccess=0x{4}, dwCreateDisposition=0x{5}, createOptions=0x{ dwCreateOptions}", ObjectID, (int)nDesiredAccess, allocationSize, 0, (int)nShareAccess, dwCreateDisposition);

retval = status;
hFile = new SafeFileHandle(handle, true);
}
Expand All @@ -597,71 +614,71 @@ long allocationSize
}

switch (retval)
{
case 0:
break;
{
case 0:
break;

case Interop.Errors.ERROR_SHARING_VIOLATION:
throw ADP.InvalidOperation(System.SRHelper.GetString(SR.SqlFileStream_FileAlreadyInTransaction));
case Interop.Errors.ERROR_SHARING_VIOLATION:
throw ADP.InvalidOperation(System.SRHelper.GetString(SR.SqlFileStream_FileAlreadyInTransaction));

case Interop.Errors.ERROR_INVALID_PARAMETER:
throw ADP.Argument(System.SRHelper.GetString(SR.SqlFileStream_InvalidParameter));
case Interop.Errors.ERROR_INVALID_PARAMETER:
throw ADP.Argument(System.SRHelper.GetString(SR.SqlFileStream_InvalidParameter));

case Interop.Errors.ERROR_FILE_NOT_FOUND:
{
System.IO.DirectoryNotFoundException e = new System.IO.DirectoryNotFoundException();
ADP.TraceExceptionAsReturnValue(e);
throw e;
}
default:
case Interop.Errors.ERROR_FILE_NOT_FOUND:
{
System.IO.DirectoryNotFoundException e = new System.IO.DirectoryNotFoundException();
ADP.TraceExceptionAsReturnValue(e);
throw e;
}
default:
{
uint error = Interop.NtDll.RtlNtStatusToDosError(retval);
if (error == ERROR_MR_MID_NOT_FOUND)
{
uint error = Interop.NtDll.RtlNtStatusToDosError(retval);
if (error == ERROR_MR_MID_NOT_FOUND)
{
// status code could not be mapped to a Win32 error code
error = (uint)retval;
}

System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(unchecked((int)error));
ADP.TraceExceptionAsReturnValue(e);
throw e;
// status code could not be mapped to a Win32 error code
error = (uint)retval;
}
}

if (hFile.IsInvalid)
{
System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(Interop.Errors.ERROR_INVALID_HANDLE);
ADP.TraceExceptionAsReturnValue(e);
throw e;
}

if (Interop.Kernel32.GetFileType(hFile) != Interop.Kernel32.FileTypes.FILE_TYPE_DISK)
{
hFile.Dispose();
throw ADP.Argument(System.SRHelper.GetString(SR.SqlFileStream_PathNotValidDiskResource));
}

// if the user is opening the SQL FileStream in read/write mode, we assume that they want to scan
// through current data and then append new data to the end, so we need to tell SQL Server to preserve
// the existing file contents.
if (access == System.IO.FileAccess.ReadWrite)
{
uint ioControlCode = Interop.Kernel32.CTL_CODE(FILE_DEVICE_FILE_SYSTEM,
IoControlCodeFunctionCode, (byte)Interop.Kernel32.IoControlTransferType.METHOD_BUFFERED,
(byte)Interop.Kernel32.IoControlCodeAccess.FILE_ANY_ACCESS);

if (!Interop.Kernel32.DeviceIoControl(hFile, ioControlCode, IntPtr.Zero, 0, IntPtr.Zero, 0, out uint cbBytesReturned, IntPtr.Zero))
{
System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(unchecked((int)error));
ADP.TraceExceptionAsReturnValue(e);
throw e;
}
}

if (hFile.IsInvalid)
{
System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(Interop.Errors.ERROR_INVALID_HANDLE);
ADP.TraceExceptionAsReturnValue(e);
throw e;
}

if (Interop.Kernel32.GetFileType(hFile) != Interop.Kernel32.FileTypes.FILE_TYPE_DISK)
{
hFile.Dispose();
throw ADP.Argument(System.SRHelper.GetString(SR.SqlFileStream_PathNotValidDiskResource));
}

// if the user is opening the SQL FileStream in read/write mode, we assume that they want to scan
// through current data and then append new data to the end, so we need to tell SQL Server to preserve
// the existing file contents.
if (access == System.IO.FileAccess.ReadWrite)
{
uint ioControlCode = Interop.Kernel32.CTL_CODE(FILE_DEVICE_FILE_SYSTEM,
IoControlCodeFunctionCode, (byte)Interop.Kernel32.IoControlTransferType.METHOD_BUFFERED,
(byte)Interop.Kernel32.IoControlCodeAccess.FILE_ANY_ACCESS);

if (!Interop.Kernel32.DeviceIoControl(hFile, ioControlCode, IntPtr.Zero, 0, IntPtr.Zero, 0, out uint cbBytesReturned, IntPtr.Zero))
{
System.ComponentModel.Win32Exception e = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
ADP.TraceExceptionAsReturnValue(e);
throw e;
}
}

// now that we've successfully opened a handle on the path and verified that it is a file,
// use the SafeFileHandle to initialize our internal System.IO.FileStream instance
System.Diagnostics.Debug.Assert(_m_fs == null);
_m_fs = new System.IO.FileStream(hFile, access, DefaultBufferSize, ((options & System.IO.FileOptions.Asynchronous) != 0));
// now that we've successfully opened a handle on the path and verified that it is a file,
// use the SafeFileHandle to initialize our internal System.IO.FileStream instance
System.Diagnostics.Debug.Assert(_m_fs == null);
_m_fs = new System.IO.FileStream(hFile, access, DefaultBufferSize, ((options & System.IO.FileOptions.Asynchronous) != 0));
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ virtual internal void DelegatedTransactionEnded()
// IMPORTANT NOTE: You must have taken a lock on the object before
// you call this method to prevent race conditions with Clear and
// ReclaimEmancipatedObjects.
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionInternal.DelegatedTransactionEnded|RES|CPOOL> {0}#, Delegated Transaction Completed.", ObjectID);
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DelegatedTransactionEnded|RES|CPOOL> {0}#, Delegated Transaction Completed.", ObjectID);

if (1 == _pooledCount)
{
Expand Down Expand Up @@ -892,7 +892,7 @@ internal void DetachCurrentTransactionIfEnded()
// Detach transaction from connection.
internal void DetachTransaction(SysTx.Transaction transaction, bool isExplicitlyReleasing)
{
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionInternal.DetachTransaction|RES|CPOOL> {0}#, Transaction Completed. (pooledCount={1})", ObjectID, _pooledCount);
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DetachTransaction|RES|CPOOL> {0}#, Transaction Completed. (pooledCount={1})", ObjectID, _pooledCount);

// potentially a multi-threaded event, so lock the connection to make sure we don't enlist in a new
// transaction between compare and assignment. No need to short circuit outside of lock, since failed comparisons should
Expand Down Expand Up @@ -933,7 +933,7 @@ internal void CleanupConnectionOnTransactionCompletion(SysTx.Transaction transac
void TransactionCompletedEvent(object sender, SysTx.TransactionEventArgs e)
{
SysTx.Transaction transaction = e.Transaction;
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionInternal.TransactionCompletedEvent|RES|CPOOL> {0}#, Transaction Completed. (pooledCount = {1})", ObjectID, _pooledCount);
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.TransactionCompletedEvent|RES|CPOOL> {0}#, Transaction Completed. (pooledCount = {1})", ObjectID, _pooledCount);

CleanupTransactionOnCompletion(transaction);
CleanupConnectionOnTransactionCompletion(transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sealed public class SqlFileStream : System.IO.Stream
// TransactionContext accessors as virtual methods. Doing so now on a sealed class
// generates a compiler error (CS0549)

// For BID tracing output
// For EventTrace output
private static int _objectTypeCount; // Bid counter
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
internal readonly int ObjectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);

Expand Down