I think that the problem stems from the action of the operator $\hat p$. Please correct me if I am mistaken.
The action of the operator $\hat p$ in the quantum space is defined as
$<x|\hat p|a>=-i \hbar \partial_x <x|a>$
if the state $|a>$ does not depend on x. In fact, if the state $|a>$ depended on $x$, like for instance $|a>=f(x)|b>$ for any scalar function $f(x)$, then clearly the equation
$<x|\hat p|a>=<x|\hat p f(x)|b>=-i \hbar \partial_x <x|f(x)|b>= -i \hbar \partial_x (f(x) <x|b>) $
would be badly defined, as it could be evaluated in another different way:
$<x|\hat p|a>=<x|\hat p f(x)|b>=f(x) <x|\hat p |b>=f(x)(-i \hbar) \partial_x <x|b>$
The second evaluation comes from the fact that, in Standard Quantum Mechanics, it is postulated that any operator acts on ket vectors and not on scalars (with the exception the Time reversal operator, which is not of any use here).
The commutator relation $\left[\hat x, \hat p\right]=i\hbar$ is obtained from the action of the operator $\hat p$ as defined above. Thus, it comes straightforwardly that such a commutation relation cannot be generally used in a scalar product ($<x|...|ket>$) if the ket state on the right depends on $x$.
Having said that, when you perform the trace of the commutator $\left[\hat x, \hat p\right]$, you are doing
$Tr\Big[\left[\hat x, \hat p\right]\Big]=\int dx <x|(\hat x\hat p-\hat p\hat x)|x>=\int dx <x|(x\hat p-\hat p x)|x>$,
where in the last step above I have just extracted the eigenvalues from the eigenstates $|x>$.
In the above equation you have a scalar product where the ket on the right depends on $x$. Thus, you'll have to be careful in the evaluation and you cannot use the $xp$-commutation relations straight away. With a little care, everyone can see from the above equation that, indeed, the trace gives zero
$\int dx <x|(x\hat p-\hat p x)|x>=\int dx \,x<x|(\hat p-\hat p )|x>=0$,
as it should.
Whereas, if you had used the $xp$-commutation relations from the outset, you would have wrongly found
$Tr\Big[\left[\hat x, \hat p\right]\Big]=Tr\Big[i\hbar\Big]=i\hbar$.
Edited after Joe's Comment
In the last equation I forgot the dimensionality of the space. It must be modified as
$Tr\Big[\left[\hat x, \hat p\right]\Big]=Tr\Big[i\hbar\Big]=i\hbar\,D$
where $D$ are the dimensions of the quantum space you are taking the trace in. Thanks Joe.