Skip to content

Instantly share code, notes, and snippets.

View ronascentes's full-sized avatar

Rodrigo Nascentes ronascentes

View GitHub Profile
@ronascentes
ronascentes / ConnectionPoolDebuggingLogicForMongoDB.cs
Created February 16, 2022 18:55
How to enable C# connection pool debugging logic for MongoDB
var clientSettings = new MongoClientSettings();
var traceSource = new TraceSource("MongoDB-CMAP-SDAM", SourceLevels.All);
traceSource.Listeners.Clear(); // remove the default listener
var logFileName = "<should be updated on the real path to a log file>";
var listener = new TextWriterTraceListener(new FileStream(logFileName, FileMode.Append));
listener.TraceOutputOptions = TraceOptions.DateTime;
traceSource.Listeners.Add(listener);
var eventSubscriber = new TraceSourceEventSubscriber(traceSource);
Action<ClusterBuilder> clusterConfigurator = builder => builder.Subscribe(eventSubscriber); // if you don't use a MongoClient as singletone, it's better to share the same instance(so object.ReferenceEquals for them should return true) of this configurator, between all mongo clients
clientSettings.ClusterConfigurator = clusterConfigurator;
@ronascentes
ronascentes / PSTop.ps1
Created January 7, 2022 17:38
Poor man's "top" for Windows
while($true) { cls ; '' ; ps | sort cpu -Descending | select -first 20 | ft -auto; Start-Sleep -Milliseconds 1000 }
@ronascentes
ronascentes / digon.json
Created September 24, 2021 18:50
Windows Terminal Digon Color Scheme
{
"background": "#1B1D1E",
"black": "#222D3F",
"blue": "#3167AC",
"brightBlack": "#a4eeb7",
"brightBlue": "#3C7DD2",
"brightCyan": "#35B387",
"brightGreen": "#2D9440",
"brightPurple": "#8230A7",
"brightRed": "#D4312E",
@ronascentes
ronascentes / if_null.ps1
Created April 20, 2021 19:46
Validating null value in powershell
# If $value is not $null or 0 or $false or an empty string
if ($null -ne $value -and $value -ne 0 -and $value -ne '' -and $value -ne $false ){
:do
}
@ronascentes
ronascentes / killAllConnections.sql
Created January 11, 2020 21:42
Kill all the current connections of a SQL instance
DECLARE @session_id INT, @sql NVARCHAR (4000);
DECLARE database_curs CURSOR FAST_FORWARD FOR
SELECT c.session_id
FROM sys.dm_exec_connections AS c
JOIN sys.dm_exec_sessions AS s ON c.session_id = s.session_id
WHERE c.session_id <> @@SPID AND s.is_user_process = 1;
OPEN database_curs;
FETCH NEXT FROM database_curs INTO @session_id;
WHILE (@@fetch_status <> -1)
BEGIN
@ronascentes
ronascentes / checkStatistics.sql
Created December 4, 2019 19:01
Use this T-SQL script to generate the complete list of tables that need statistics update in a given database
;WITH StatTables AS(
SELECT so.schema_id AS 'schema_id', so.name AS 'TableName', so.object_id AS 'object_id', ISNULL(sp.rows,0) AS 'ApproximateRows', ISNULL(sp.modification_counter,0) AS 'RowModCtr'
FROM sys.objects so (NOLOCK) JOIN sys.stats st (NOLOCK) ON so.object_id=st.object_id
CROSS APPLY sys.dm_db_stats_properties(so.object_id, st.stats_id) AS sp
WHERE so.is_ms_shipped = 0 AND st.stats_id<>0
AND so.object_id NOT IN (SELECT major_id FROM sys.extended_properties (NOLOCK) WHERE name = N'microsoft_database_tools_support')
),
StatTableGrouped AS(
SELECT ROW_NUMBER() OVER(ORDER BY TableName) AS seq1, ROW_NUMBER() OVER(ORDER BY TableName DESC) AS seq2, TableName, cast(Max(ApproximateRows) AS bigint) AS ApproximateRows,
cast(Max(RowModCtr) AS bigint) AS RowModCtr, count(*) AS StatCount, schema_id,object_id
@ronascentes
ronascentes / findHostNameFromIpAddress.ps1
Created February 20, 2019 18:16
Find host name from IP address
$ipAddress= "192.168.1.54"
[System.Net.Dns]::GetHostByAddress($ipAddress).Hostname
@ronascentes
ronascentes / get_exec_times.sql
Created January 16, 2019 17:29
Get execution times
SELECT DB_NAME(database_id) DatabaseName,
OBJECT_NAME(object_id) ProcedureName,
cached_time, last_execution_time, execution_count,
total_elapsed_time/execution_count AS avg_elapsed_time,
type_desc, datetime=getdate()
FROM sys.dm_exec_procedure_stats
where database_id=6
and OBJECT_NAME(object_id)='ProcExtendedSessionUpsertV5'
@ronascentes
ronascentes / check_connectivity_errors.sql
Created January 3, 2019 17:23
Check network connectivity errors
;WITH RingBufferConnectivity as
( SELECT
records.record.value('(/Record/@id)[1]', 'int') AS [RecordID],
records.record.value('(/Record/ConnectivityTraceRecord/RecordType)[1]', 'varchar(max)') AS [RecordType],
records.record.value('(/Record/ConnectivityTraceRecord/RecordTime)[1]', 'datetime') AS [RecordTime],
records.record.value('(/Record/ConnectivityTraceRecord/SniConsumerError)[1]', 'int') AS [Error],
records.record.value('(/Record/ConnectivityTraceRecord/State)[1]', 'int') AS [State],
records.record.value('(/Record/ConnectivityTraceRecord/Spid)[1]', 'int') AS [Spid],
records.record.value('(/Record/ConnectivityTraceRecord/RemoteHost)[1]', 'varchar(max)') AS [RemoteHost],
records.record.value('(/Record/ConnectivityTraceRecord/RemotePort)[1]', 'varchar(max)') AS [RemotePort],
@ronascentes
ronascentes / errorHandling.ps1
Last active August 17, 2018 16:52
Powershell Generic Error Handling
try{
Get-Childitem c:\Foo -ErrorAction stop
}
catch [System.Management.Automation.ItemNotFoundException]{
'oops, I guess that folder was not there'
}